package com.ibangnuo.manage.service.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ibangnuo.framework.domain.*;
import com.ibangnuo.framework.domain.request.order.OrderQueryCountRequest;
import com.ibangnuo.framework.domain.request.order.OrderQueryGroupRequest;
import com.ibangnuo.framework.domain.request.order.OrderQueryRequest;
import com.ibangnuo.framework.exception.ExceptionCast;
import com.ibangnuo.framework.model.response.CommonCode;
import com.ibangnuo.framework.model.response.ObjectResult;
import com.ibangnuo.framework.model.response.QueryResponseResult;
import com.ibangnuo.framework.model.response.QueryResult;
import com.ibangnuo.manage.dao.*;
import com.ibangnuo.manage.dao.employee.*;
import com.ibangnuo.manage.dao.shop.MerchantsRepository;
import com.ibangnuo.manage.dao.terminal.TerminalRepository;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: wwy
 * @Date: 2019-12-31 13:58
 */
@Service
public class OrderService {

    private static Logger LOGGER = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private CommercialRepository commercialRepository;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AgentMapper agentMapper;
    @Autowired
    private OrderDetailsRepository orderDetailsRepository;

    @Autowired
    private AgentRepository agentRepository;

    @Autowired
    private MerchantsRepository merchantsRepository;

    @Autowired
    private OrderTotalRepository orderTotalRepository;
    @Autowired
    private OrderTotalMapper orderTotalMapper;
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private TerminalRepository terminalRepository;

    @Autowired
    private OrderTotalMonthRepository orderTotalMonthRepository;

    /**
     * 分页条件查询订单
     * 1.权限控制
     * 商家
     * 代理商
     * 小程序
     *
     * @param orderQueryRequest
     * @return
     */
    public QueryResponseResult findList(OrderQueryRequest orderQueryRequest) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> commercialTenantIdList = null;
        Integer type = 98;
        if (user == null) {
            //小程序登录，需要sn号
            if (StringUtils.isEmpty(orderQueryRequest.getSn())) {
                ExceptionCast.cast(CommonCode.FAIL);
            }
        } else {
            type = user.getType();
            /*if (type == 1) {//代理商，查询出下面的商户*/

            commercialTenantIdList = this.findCommercial(user.getId());
            if (type == 1) {
                //代理商，查询出下面的商户
                //查询出代理商id
                Agent agent = agentRepository.findByUserId(user.getId());
                commercialTenantIdList = this.findCommercial(agent.getId());
            }
            if (type == 2) {//商户
                CommercialTenant byUserId = commercialRepository.findByUserId(user.getId());
                if (byUserId == null) {
                    ExceptionCast.cast(CommonCode.FAIL);
                }
                orderQueryRequest.setCommercialTenantId(byUserId.getId());
            }
            if (type == 3) {    //门店,依据sn号查询
                if (StringUtils.isEmpty(orderQueryRequest.getSn())) {
                    ExceptionCast.cast(CommonCode.FAIL);
                }
            }
            if (type == 0) {    //管理员
                List<CommercialTenant> all = commercialRepository.findAll();
                for (CommercialTenant commercialTenant : all) {
                    commercialTenantIdList.add(commercialTenant.getId());
                }
            }

            if (type == 4) { //员工查询
                List<CommercialTenant> list = commercialRepository.findByEmployeeId(user.getId());
                for (CommercialTenant commercialTenant : list) {
                    commercialTenantIdList.add(commercialTenant.getId());
                }
            }

            if (type == 5) { //店员查询
                String shopId = user.getShopId();
                if (shopId != null && orderQueryRequest.getShopId() == null) {
                    orderQueryRequest.setShopId(shopId);
                }
            }
        }

        if (orderQueryRequest == null) {
            new OrderQueryRequest();
        }
        Date startTime = null;
        Date endTime = null;

        if (orderQueryRequest.getDateTime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTimeStr = simpleDateFormat.format(orderQueryRequest.getDateTime());
            try {
                startTime = simpleDateFormat.parse(startTimeStr);
                long time = startTime.getTime();
                long endTimeLong = time + (24 * 60 * 60 * 1000);
                endTime = new Date(endTimeLong);
            } catch (ParseException e) {
                LOGGER.error("时间格式转换异常:{}", e.getMessage());
            }
        }
        Date finalStartTime = startTime;
        Date finalEndTime = endTime;

        Date finalEndTimes = orderQueryRequest.getEndTime();
        Date finalStartTimes = orderQueryRequest.getStartTime();

        List<String> finalCommercialTenantIdList = commercialTenantIdList;
        Integer finalType = type;
        Specification specification = (var1, var2, var3) -> {
            Predicate predicate = var3.conjunction();
            if (finalEndTimes != null && finalStartTimes != null) {
                //后台登录起始日期
                predicate.getExpressions().add(var3.greaterThanOrEqualTo(var1.get("createTime").as(Date.class), finalStartTimes));
                //结束日期
                predicate.getExpressions().add(var3.lessThanOrEqualTo(var1.get("createTime").as(Date.class), finalEndTimes));
            }
            if (StringUtils.isNotEmpty(orderQueryRequest.getSn())) {
                predicate.getExpressions().add(var3.equal(var1.get("sn"), orderQueryRequest.getSn()));
            }
            //支付类型
            if (StringUtils.isNotEmpty(orderQueryRequest.getPayType())) {
                predicate.getExpressions().add(var3.equal(var1.get("payType"), orderQueryRequest.getPayType()));
            }
            //支付状态
            if (orderQueryRequest.getStatus() != null) {
                predicate.getExpressions().add(var3.equal(var1.get("status"), orderQueryRequest.getStatus()));
            }
            //支付方式
            if (StringUtils.isNotEmpty(orderQueryRequest.getPayWay())) {
                predicate.getExpressions().add(var3.equal(var1.get("payWay"), orderQueryRequest.getPayWay()));
            }
            //订单号
            if (StringUtils.isNotEmpty(orderQueryRequest.getOutTradeNo())) {
                predicate.getExpressions().add(var3.equal(var1.get("outTradeNo"), orderQueryRequest.getOutTradeNo()));
            }
            //流水号

            if (StringUtils.isNotEmpty(orderQueryRequest.getSerialNumber())) {
                predicate.getExpressions().add(var3.equal(var1.get("serialNumber"), orderQueryRequest.getSerialNumber()));
            }
            //门店id

            if (StringUtils.isNotEmpty(orderQueryRequest.getShopId())) {
                predicate.getExpressions().add(var3.equal(var1.get("shopId"), orderQueryRequest.getShopId()));
            }
            if (finalEndTime != null && finalStartTime != null) {
                //起始日期
                predicate.getExpressions().add(var3.greaterThanOrEqualTo(var1.get("createTime").as(Date.class), finalStartTime));
                //结束日期
                predicate.getExpressions().add(var3.lessThanOrEqualTo(var1.get("createTime").as(Date.class), finalEndTime));
            }


            if (finalType == 1 || finalType == 0 || finalType == 4) {//代理商，管理员，员工
                //商家id条件
                CriteriaBuilder.In<String> in = var3.in(var1.get("commercialTenantId"));
                if (finalCommercialTenantIdList != null && finalCommercialTenantIdList.size() > 0) {
                    for (String s : finalCommercialTenantIdList) {
                        in.value(s);
                    }
                } else {
                    in.value("0");
                }
                predicate.getExpressions().add(in);
            }

            if (finalType == 2) {//商户
                predicate.getExpressions().add(var3.equal(var1.get("commercialTenantId"), orderQueryRequest.getCommercialTenantId()));
            }
            return predicate;
        };

        int page = orderQueryRequest.getPage();
        int size = orderQueryRequest.getLimit();
        if (page <= 0) {
            page = 1;
        }
        page = page - 1;
        if (size <= 0) {
            size = 10;
        }
        //page = page -1;
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page, size, sort);
        QueryResult queryResult = new QueryResult();
//        List<Order> all1 = orderRepository.findAll(specification);

        Page<Order> all = orderRepository.findAll(specification, pageable);

        for (Order order : all.getContent()) {
            String shopId = String.valueOf(order.getShopId());
            Optional<Merchants> byId = merchantsRepository.findById(shopId);
            if (byId.isPresent()) {
                String name = byId.get().getName();
                order.setShopId(name);
            }
            String sn = String.valueOf(order.getSn());
            Terminal bySn = terminalRepository.findBySn(sn);
            if (bySn != null) {
                String name = bySn.getName();
                order.setSn(name);
            }
        }
        queryResult.setTotal(all.getTotalElements());
        queryResult.setList(all.getContent());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }


    /**
     * 汇总查询
     *
     * @param orderQueryGroupRequest
     * @return
     */
    public ObjectResult queryGroup(OrderQueryGroupRequest orderQueryGroupRequest) {
        int page = orderQueryGroupRequest.getPage();
        int size = orderQueryGroupRequest.getSize();
        if (page<=0){
            page=1;
        }
        if (size<=0){
            size=5;
        }
        orderQueryGroupRequest.setPayWay(0);//支付宝
        PageHelper.startPage(page,size);
        List<Map> aliList = orderMapper.queryGroup(orderQueryGroupRequest);
       PageInfo<Map> pageInd=new PageInfo<>(aliList);
        orderQueryGroupRequest.setPayWay(1);//微信
        List<Map> wecharList = orderMapper.queryGroup(orderQueryGroupRequest);
        PageInfo<Map> pageInfo=new PageInfo<>(wecharList);
        Map<String, Object> map = new HashMap<>();
        map.put("aliList", pageInd.getList());
        map.put("wecharList", pageInfo.getList());
        return new ObjectResult(CommonCode.SUCCESS, map);
    }

    /**
     * 根据代理商id查询出下面的商户
     *
     * @param agentId
     * @return
     */
    List<String> findCommercial(String agentId) {
        List<String> commercialIdList = new ArrayList<>();
        List<CommercialTenant> list = commercialRepository.findByAgentIdAndIsDelete(agentId, 1);
        for (CommercialTenant commercialTenant : list) {
            commercialIdList.add(commercialTenant.getId());
        }
        return commercialIdList;
    }

    /**
     * 更具订单id查询
     *
     * @param id
     * @return
     */
    public ObjectResult findById(String id) {
        Optional<Order> optional = orderRepository.findById(id);
        if (optional.isPresent()) {
            return new ObjectResult(CommonCode.SUCCESS, optional.get());
        }
        return new ObjectResult(CommonCode.FAIL, null);
    }

    /**
     * 更具订单id查账单详情
     *
     * @param id
     * @return
     */
    public QueryResponseResult findByOrderId(String id) {
        String orderId = orderRepository.findById(id).get().getId();

        OrderDetails orderDetails = orderDetailsRepository.findByOrderId(orderId);
        String content1 = orderDetails.getContent();
        Map<String, String> map = JSONObject.parseObject(content1, Map.class);

        Map result = new HashMap();
        String buyer_logon_id = map.get("buyerLogonId");
        result.put("buyerLogonId", buyer_logon_id);
        String buyer_pay_amount = map.get("buyerPayAmount");
        result.put("buyerPayAmount", buyer_pay_amount);
        String nvoice_amount = map.get("invoiceAmount");
        result.put("invoiceAmount", nvoice_amount);
        String out_trade_no = map.get("outTradeNo");
        result.put("outTradeNo", out_trade_no);
        String receipt_amount = map.get("receiptAmount");
        result.put("receiptAmount", receipt_amount);
        String total_amount = map.get("totalAmount");
        result.put("totalAmount", total_amount);
        String trade_no = map.get("tradeNo");
        result.put("tradeNo", trade_no);

//        String gmtPayment = map.get("gmtPayment");
        /*Date date=new Date((gmtPayment));
        result.put("gmtPayment", gmtPayment);*/
        List<Map> all = new ArrayList();
        all.add(result);

        QueryResult queryResult = new QueryResult();
        queryResult.setTotal(all.size());
        queryResult.setList(all);
        if (orderDetails != null) {
            return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //凌晨1点自动执行前天的汇总
    @Scheduled(cron = "* * 1 * * ?")
    public void count() {
        TerminalCount();//机具汇总
        ShopCount();//门店汇总
        CountEmployee();//员工汇总
        CommercialCount();//商户汇总
        CountAgent();//代理商汇总

    }

    //机具的汇总
    public void TerminalCount() {
        LOGGER.info("凌晨1点TerminalCount方法"+new Date());
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String format = sdf.format(time);
        String startTime = sdf.format(time) + " 00:00:00";

        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //查询出所有的机具sn号
        List<Terminal> all = terminalRepository.findAll();
        for (Terminal terminal : all) {
            if (terminal.getSn() != null) {
                String sn = terminal.getSn();
                map.put("sn", sn);
                //微信
                Map ConsumeWxMap = orderMapper.countConsumelWx(map);//按时间的微信订单总金额和条数
                Map mapTuiWx = orderMapper.tuiCountWx(map);//退款总金额 和条数
                Map mapActualWx = orderMapper.countActualWx(map);//实收总金额 和条数
                Map mapDiscountsWx = orderMapper.discountsCountWx(map); //折扣总金额

                OrderTotal orderTotalWx = new OrderTotal();
                orderTotalWx.setSn(sn);
                orderTotalWx.setPayWay(1);
                if (String.valueOf(mapTuiWx.get("totalTui")) == "null") {
                    orderTotalWx.setTuiNum("0");//退款条数
                } else {
                    orderTotalWx.setTuiNum(String.valueOf(mapTuiWx.get("totalTui")));//退款条数
                }
                if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null") {
                    orderTotalWx.setTuiMoney("0");//退款金额
                } else {
                    orderTotalWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));//退款金额
                }

                if (String.valueOf(mapActualWx.get("actualMoney")) == "null") {
                    orderTotalWx.setTotalMoney("0");//实付金额
                } else {
                    orderTotalWx.setTotalMoney(String.valueOf(mapActualWx.get("actualMoney")));//结算金额，实收金额
                }
                orderTotalWx.setTotal(String.valueOf(mapActualWx.get("totalActual")));//总条数

                if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null") {
                    orderTotalWx.setDiscountsMoney("0");//退款金额
                } else {
                    orderTotalWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));//折扣金额
                }
                if (String.valueOf(ConsumeWxMap.get("consumeMoney")) == "null") {
                    orderTotalWx.setConsumeMoney("0");
                } else {
                    orderTotalWx.setConsumeMoney(String.valueOf(ConsumeWxMap.get("consumeMoney")));//订单总金额
                }

                String actualMoneyWx = String.valueOf(mapActualWx.get("actualMoney"));//实收金额
                String discountsMoneyWx = String.valueOf(mapTuiWx.get("tuiMoney"));//退款金额
                Integer settleAccounts = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccounts = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccounts = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccounts = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccounts = actualMoney + discountsMoney;
                }
                orderTotalWx.setSettleAccounts(String.valueOf(settleAccounts));

                orderTotalWx.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalWx);//保存到ordertotal表中

                //支付宝
                Map ConsumeAlipayMap = orderMapper.countConsumelAlipay(map);//按时间的微信订单总金额和条数
                Map mapTuiAlipay = orderMapper.tuiCountAlipay(map);//退款总金额 和条数
                Map mapActualAlipay = orderMapper.countActualAlipay(map);//实收总金额 和条数
                Map mapDiscountsAlipay = orderMapper.discountsCountAlipay(map); //折扣总金额

                OrderTotal orderTotalAlipay = new OrderTotal();
                orderTotalAlipay.setSn(sn);
                orderTotalAlipay.setPayWay(0);
                if (String.valueOf(mapTuiAlipay.get("totalTui")) == "null") {
                    orderTotalAlipay.setTuiNum("0");//退款条数
                } else {
                    orderTotalAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("totalTui")));//退款条数
                }
                if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null") {
                    orderTotalAlipay.setTuiMoney("0");//退款金额
                } else {
                    orderTotalAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                }

                if (String.valueOf(mapActualAlipay.get("actualMoney")) == "null") {
                    orderTotalAlipay.setTotalMoney("0");//实付金额
                } else {
                    orderTotalAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("actualMoney")));//结算金额，实收金额
                }
                orderTotalAlipay.setTotal(String.valueOf(mapActualAlipay.get("totalActual")));//总条数

                if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null") {
                    orderTotalAlipay.setDiscountsMoney("0");//退款金额
                } else {
                    orderTotalAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                }
                if (String.valueOf(ConsumeAlipayMap.get("consumeMoney")) == "null") {
                    orderTotalAlipay.setConsumeMoney("0");
                } else {
                    orderTotalAlipay.setConsumeMoney(String.valueOf(ConsumeAlipayMap.get("consumeMoney")));//订单总金额
                }

                String actualMoneyAlipay = String.valueOf(mapActualAlipay.get("actualMoney"));//实收金额
                String discountsMoneyAlipay = String.valueOf(mapTuiAlipay.get("tuiMoney"));//退款金额
                Integer settleAccountsAlipay = 0;//订单金额
                if (actualMoneyAlipay == "null" && discountsMoneyAlipay == "null") {
                    settleAccountsAlipay = 0;
                }
                if (actualMoneyAlipay == "null" && discountsMoneyAlipay != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyAlipay);
                    settleAccountsAlipay = discountsMoney;
                }
                if (actualMoneyAlipay != "null" && discountsMoneyAlipay == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyAlipay);//实收金额
                    settleAccountsAlipay = actualMoney;
                }
                if (actualMoneyAlipay != "null" && discountsMoneyAlipay != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyAlipay);
                    int actualMoney = Integer.parseInt(actualMoneyAlipay);//实收金额
                    settleAccountsAlipay = actualMoney + discountsMoney;
                }
                orderTotalAlipay.setSettleAccounts(String.valueOf(settleAccountsAlipay));

                orderTotalAlipay.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalAlipay);//保存到ordertotal表中

                //微信+支付宝的
                OrderTotal orderTotal = new OrderTotal();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalAlipay.getTuiMoney() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiMoney());
                }
                if (orderTotalWx.getTuiMoney() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalAlipay.getTuiNum() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiNum());
                }
                if (orderTotalWx.getTuiNum() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalAlipay.getTotal() == "null") {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalAlipay.getTotal());
                }
                if (orderTotalWx.getTotal() == "null") {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalAlipay.getTotalMoney() == "null") {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalAlipay.getTotalMoney());
                }
                if (orderTotalWx.getTotalMoney() == "null") {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalAlipay.getDiscountsMoney() == "null") {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalAlipay.getDiscountsMoney());
                }
                if (orderTotalWx.getDiscountsMoney() == "null") {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalAlipay.getSettleAccounts() == "null") {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalAlipay.getSettleAccounts());
                }
                if (orderTotalWx.getSettleAccounts() == "null") {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalAlipay.getConsumeMoney() == "null") {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalAlipay.getConsumeMoney());
                }
                if (orderTotalWx.getConsumeMoney() == "null") {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalWx.getConsumeMoney());
                }
                orderTotal.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotal.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotal.setCreateTime(format);
                orderTotal.setSn(sn);
                orderTotal.setTotal(String.valueOf(totalA + totalW));
                orderTotal.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotal.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotal.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotal.setPayWay(2);
                orderTotalRepository.save(orderTotal);//商户的微信支付宝日汇总保存到ordertotal表中
            }

        }


    }

    //门店的汇总
    public void ShopCount() {
        LOGGER.info("凌晨1点ShopCount方法"+new Date());
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String format = sdf.format(time);

        String startTime = sdf.format(time) + " 00:00:00";

        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //查询所有门店id
        List<Merchants> all = merchantsRepository.findAll();
        for (Merchants merchants : all) {
            if (merchants.getId() != null) {
                String merchantsId = merchants.getId();
//                map.put("merchantsId", merchantsId);
                map.put("shopId", merchantsId);
                //按时间微信汇总
                Map mapConsumeWx = orderMapper.consumeShopWx(map);//该门店的订单总金额和条数
                Map mapTuiWx = orderMapper.tuiShopWx(map);//退款总金额 和条数
                Map mapActualWx = orderMapper.actualShopWx(map);//实收总金额 和条数
                Map mapDiscountsWx = orderMapper.discountsShopWx(map); //折扣总金额
                OrderTotal orderTotalWx = new OrderTotal();
                orderTotalWx.setShopId(merchants.getId());//门店id
                orderTotalWx.setPayWay(1);//支付方式
                if (String.valueOf(mapTuiWx.get("totalTui")) == "null") {
                    orderTotalWx.setTuiNum("0");//退款条数
                } else {
                    orderTotalWx.setTuiNum(String.valueOf(mapTuiWx.get("totalTui")));//退款条数
                }
                if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null") {
                    orderTotalWx.setTuiMoney("0");//退款金额
                } else {
                    orderTotalWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));//退款金额
                }

                if (String.valueOf(mapActualWx.get("actualMoney")) == "null") {
                    orderTotalWx.setTotalMoney("0");//实付金额
                } else {
                    orderTotalWx.setTotalMoney(String.valueOf(mapActualWx.get("actualMoney")));//结算金额，实收金额
                }
                orderTotalWx.setTotal(String.valueOf(mapActualWx.get("totalActual")));//总条数

                if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null") {
                    orderTotalWx.setDiscountsMoney("0");//退款金额
                } else {
                    orderTotalWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));//折扣金额
                }
                if (String.valueOf(mapConsumeWx.get("consumeMoney")) == "null") {
                    orderTotalWx.setConsumeMoney("0");
                } else {
                    orderTotalWx.setConsumeMoney(String.valueOf(mapConsumeWx.get("consumeMoney")));//订单总金额
                }

                String actualMoneyWx = String.valueOf(mapActualWx.get("actualMoney"));//实收金额
                String discountsMoneyWx = String.valueOf(mapTuiWx.get("tuiMoney"));//退款金额
                Integer settleAccountsWx = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccountsWx = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccountsWx = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney + discountsMoney;
                }
                orderTotalWx.setSettleAccounts(String.valueOf(settleAccountsWx));

                orderTotalWx.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalWx);//保存到ordertotal表中

                //按时间支付宝汇总
                Map mapConsumeAlipay = orderMapper.consumeShopAlipay(map);//该门店的订单总金额和条数
                Map mapTuiAlipay = orderMapper.tuiShopAlipay(map);//退款总金额 和条数
                Map mapActualAlipay = orderMapper.actualShopAlipay(map);//实收总金额 和条数
                Map mapDiscountsAlipay = orderMapper.discountsShopAlipay(map); //折扣总金额
                OrderTotal orderTotalAlipay = new OrderTotal();
                orderTotalAlipay.setShopId(merchants.getId());//门店id
                orderTotalAlipay.setPayWay(0);
                if (String.valueOf(mapTuiAlipay.get("totalTui")) == "null") {
                    orderTotalAlipay.setTuiNum("0");//退款条数
                } else {
                    orderTotalAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("totalTui")));//退款条数
                }
                if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null") {
                    orderTotalAlipay.setTuiMoney("0");//退款金额
                } else {
                    orderTotalAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                }

                if (String.valueOf(mapActualAlipay.get("actualMoney")) == "null") {
                    orderTotalAlipay.setTotalMoney("0");//实付金额
                } else {
                    orderTotalAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("actualMoney")));//结算金额，实收金额
                }
                orderTotalAlipay.setTotal(String.valueOf(mapActualAlipay.get("totalActual")));//总条数

                if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null") {
                    orderTotalAlipay.setDiscountsMoney("0");//退款金额
                } else {
                    orderTotalAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                }
                if (String.valueOf(mapConsumeAlipay.get("consumeMoney")) == "null") {
                    orderTotalAlipay.setConsumeMoney("0");
                } else {
                    orderTotalAlipay.setConsumeMoney(String.valueOf(mapConsumeAlipay.get("consumeMoney")));//订单总金额
                }

                String actualMoneyAlipay = String.valueOf(mapActualAlipay.get("actualMoney"));//实收金额
                String discountsMoneyAlipay = String.valueOf(mapTuiAlipay.get("tuiMoney"));//退款金额
                Integer settleAccountsAlipay = 0;//订单金额
                if (actualMoneyAlipay == "null" && discountsMoneyAlipay == "null") {
                    settleAccountsAlipay = 0;
                }
                if (actualMoneyAlipay == "null" && discountsMoneyAlipay != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyAlipay);
                    settleAccountsAlipay = discountsMoney;
                }
                if (actualMoneyAlipay != "null" && discountsMoneyAlipay == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyAlipay);//实收金额
                    settleAccountsAlipay = actualMoney;
                }
                if (actualMoneyAlipay != "null" && discountsMoneyAlipay != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyAlipay);
                    int actualMoney = Integer.parseInt(actualMoneyAlipay);//实收金额
                    settleAccountsAlipay = actualMoney + discountsMoney;
                }
                orderTotalAlipay.setSettleAccounts(String.valueOf(settleAccountsAlipay));

                orderTotalAlipay.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalAlipay);//保存到ordertotal表中

                //微信+支付宝的
                OrderTotal orderTotal = new OrderTotal();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalAlipay.getTuiMoney() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiMoney());
                }
                if (orderTotalWx.getTuiMoney() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalAlipay.getTuiNum() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiNum());
                }
                if (orderTotalWx.getTuiNum() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalAlipay.getTotal() == "null") {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalAlipay.getTotal());
                }
                if (orderTotalWx.getTotal() == "null") {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalAlipay.getTotalMoney() == "null") {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalAlipay.getTotalMoney());
                }
                if (orderTotalWx.getTotalMoney() == "null") {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalAlipay.getDiscountsMoney() == "null") {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalAlipay.getDiscountsMoney());
                }
                if (orderTotalWx.getDiscountsMoney() == "null") {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalAlipay.getSettleAccounts() == "null") {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalAlipay.getSettleAccounts());
                }
                if (orderTotalWx.getSettleAccounts() == "null") {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalAlipay.getConsumeMoney() == "null") {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalAlipay.getConsumeMoney());
                }
                if (orderTotalWx.getConsumeMoney() == "null") {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalWx.getConsumeMoney());
                }
                orderTotal.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotal.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotal.setCreateTime(format);
                orderTotal.setShopId(merchantsId);
                orderTotal.setTotal(String.valueOf(totalA + totalW));
                orderTotal.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotal.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotal.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotal.setPayWay(2);
                orderTotalRepository.save(orderTotal);//商户的微信支付宝日汇总保存到ordertotal表中

            }
        }
    }

    //商户汇总
    public void CommercialCount() {
        LOGGER.info("凌晨1点CommercialCount方法"+new Date());
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String format = sdf.format(time);
        String startTime = sdf.format(time) + " 00:00:00";
        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //查询所有商户id
        List<CommercialTenant> all = commercialRepository.findAll();
        for (CommercialTenant commercialTenant : all) {
            if (commercialTenant.getId() != null) {
                String commercialTenantId = commercialTenant.getId();
                map.put("commercialTenantId", commercialTenantId);
                //按时间汇总
                Map mapConsumeWx = orderMapper.tConsumeCountWx(map);//该门店的订单总金额和条数
                Map mapActualWx = orderMapper.tActualCountWx(map);//实收总金额 和条数
                Map mapTuiWx = orderMapper.tTuiCountWx(map);//退款总金额 和条数
                Map mapDiscountsWx = orderMapper.tDiscountsCountWx(map); //折扣总金额
                OrderTotal orderTotalWx = new OrderTotal();
                orderTotalWx.setCommercialId(commercialTenant.getId());//商户id
                orderTotalWx.setPayWay(1);
                if (mapTuiWx != null) {
                    if (String.valueOf(mapTuiWx.get("totalTui")) == "null") {
                        orderTotalWx.setTuiNum("0");
                    } else {
                        orderTotalWx.setTuiNum(String.valueOf(mapTuiWx.get("totalTui")));//退款条数
                    }
                    if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null") {
                        orderTotalWx.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));//退款金额
                    }
                }
                if (mapActualWx != null) {
                    if (String.valueOf(mapActualWx.get("totalActual")) == "null") {
                        orderTotalWx.setTotal("0");
                    } else {
                        orderTotalWx.setTotal(String.valueOf(mapActualWx.get("totalActual")));//总条数
                    }
                    if (String.valueOf(mapActualWx.get("actualMoney")) == "null") {
                        orderTotalWx.setTotalMoney("0");
                    } else {
                        orderTotalWx.setTotalMoney(String.valueOf(mapActualWx.get("actualMoney")));//实收金额
                    }
                }
                if (mapDiscountsWx != null) {
                    if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null") {
                        orderTotalWx.setDiscountsMoney("0");
                    } else {
                        orderTotalWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));//折扣金额
                    }
                }
                if (mapConsumeWx != null) {
                    if (String.valueOf(mapConsumeWx.get("consumeMoney")) == "null") {
                        orderTotalWx.setConsumeMoney("0");
                    } else {
                        orderTotalWx.setConsumeMoney(String.valueOf(mapConsumeWx.get("consumeMoney")));//订单总金额
                    }
                }

                String actualMoneyWx = String.valueOf(mapActualWx.get("actualMoney"));//实收金额
                String discountsMoneyWx = String.valueOf(mapTuiWx.get("tuiMoney"));//退款金额
                Integer settleAccountsWx = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccountsWx = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccountsWx = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney + discountsMoney;
                }
                orderTotalWx.setSettleAccounts(String.valueOf(settleAccountsWx));

                orderTotalWx.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalWx);//保存到ordertotal表中


                //按支付宝时间汇总
                Map mapConsumeAlipay = orderMapper.tConsumeCountAlipay(map);//该门店的订单总金额和条数
                Map mapActualAlipay = orderMapper.tActualCountAlipay(map);//实收总金额 和条数
                Map mapTuiAlipay = orderMapper.tTuiCountAlipay(map);//退款总金额 和条数
                Map mapDiscountsAlipay = orderMapper.tDiscountsCountAlipay(map); //折扣总金额
                OrderTotal orderTotalAlipay = new OrderTotal();
                orderTotalAlipay.setCommercialId(commercialTenant.getId());//商户id
                orderTotalAlipay.setPayWay(0);
                if (mapTuiAlipay != null) {
                    if (String.valueOf(mapTuiAlipay.get("totalTui")) == "null") {
                        orderTotalAlipay.setTuiNum("0");
                    } else {
                        orderTotalAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("totalTui")));//退款条数
                    }
                    if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null") {
                        orderTotalAlipay.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                    }
                }
                if (mapActualAlipay != null) {
                    if (String.valueOf(mapActualAlipay.get("totalActual")) == "null") {
                        orderTotalAlipay.setTotal("0");
                    } else {
                        orderTotalAlipay.setTotal(String.valueOf(mapActualAlipay.get("totalActual")));//总条数
                    }
                    if (String.valueOf(mapActualAlipay.get("actualMoney")) == "null") {
                        orderTotalAlipay.setTotalMoney("0");
                    } else {
                        orderTotalAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("actualMoney")));//实收金额
                    }
                }
                if (mapDiscountsAlipay != null) {
                    if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null") {
                        orderTotalAlipay.setDiscountsMoney("0");
                    } else {
                        orderTotalAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                    }
                }
                if (mapConsumeAlipay != null) {
                    if (String.valueOf(mapConsumeAlipay.get("consumeMoney")) == "null") {
                        orderTotalAlipay.setConsumeMoney("0");
                    } else {
                        orderTotalAlipay.setConsumeMoney(String.valueOf(mapConsumeAlipay.get("consumeMoney")));//订单总金额
                    }
                }

                String actualMoney1 = String.valueOf(mapActualAlipay.get("actualMoney"));//实收金额
                String discountsMoney1 = String.valueOf(mapTuiAlipay.get("tuiMoney"));//退款金额
                Integer settleAccounts = 0;//订单金额
                if (actualMoney1 == "null" && discountsMoney1 == "null") {
                    settleAccounts = 0;
                }
                if (actualMoney1 == "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    settleAccounts = discountsMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 == "null") {
                    int actualMoney = Integer.parseInt(actualMoney1);//实收金额
                    settleAccounts = actualMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    int actualMoney = Integer.parseInt(actualMoney1);//实收金额
                    settleAccounts = actualMoney + discountsMoney;
                }
                orderTotalAlipay.setSettleAccounts(String.valueOf(settleAccounts));

                orderTotalAlipay.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalAlipay);//保存到ordertotal表中

                //微信+支付宝的
                OrderTotal orderTotal = new OrderTotal();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalAlipay.getTuiMoney() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiMoney());
                }
                if (orderTotalWx.getTuiMoney() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalAlipay.getTuiNum() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiNum());
                }
                if (orderTotalWx.getTuiNum() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalAlipay.getTotal() == "null") {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalAlipay.getTotal());
                }
                if (orderTotalWx.getTotal() == "null") {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalAlipay.getTotalMoney() == "null") {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalAlipay.getTotalMoney());
                }
                if (orderTotalWx.getTotalMoney() == "null") {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalAlipay.getDiscountsMoney() == "null") {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalAlipay.getDiscountsMoney());
                }
                if (orderTotalWx.getDiscountsMoney() == "null") {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalAlipay.getSettleAccounts() == "null") {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalAlipay.getSettleAccounts());
                }
                if (orderTotalWx.getSettleAccounts() == "null") {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalAlipay.getConsumeMoney() == "null") {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalAlipay.getConsumeMoney());
                }
                if (orderTotalWx.getConsumeMoney() == "null") {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalWx.getConsumeMoney());
                }
                orderTotal.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotal.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotal.setCreateTime(format);
                orderTotal.setCommercialId(commercialTenantId);
                orderTotal.setTotal(String.valueOf(totalA + totalW));
                orderTotal.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotal.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotal.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotal.setPayWay(2);
                orderTotalRepository.save(orderTotal);//商户的微信支付宝日汇总保存到ordertotal表中

            }
        }
    }

    //旧版的商户汇总
    public void CountCommercial() {
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String startTime = sdf.format(time) + " 00:00:00";
        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        //查询所有的员工
        List<Employee> all = employeeRepository.findAll();
        for (Employee employee1 : all) {//遍历所有的员工
            String employee1Id = employee1.getId();
            map.put("employee1Id", employee1Id);
            //按时间汇总
            List<OrderTotal> commercialCount1 = orderTotalMapper.CountCommercial1(map);//查询出员工下的所有商户的账单
            //            List<OrderTotal> commercialCount = orderTotalMapper.CountCommercial(employee1Id);//查询出员工下的所有商户的账单
            int total = 0;//总条数
            int totalMoney = 0;//实收金额
            int tuiMoney = 0;//退款金额
            int tuiNum = 0;//退款条数
            int settleAccounts = 0;//订单金额

            for (OrderTotal orderTotal : commercialCount1) {
                String total1 = orderTotal.getTotal();//总条数
                if (total1 != "null") {
                    total = total + Integer.parseInt(total1);//总条数
                }
                String totalMoney1 = orderTotal.getTotalMoney();//实收金额
                if (totalMoney1 != "null") {
                    totalMoney = totalMoney + Integer.parseInt(totalMoney1);
                }
                String tuiMoney1 = orderTotal.getTuiMoney();//退款金额
                if (tuiMoney1 != "null") {
                    tuiMoney = totalMoney + Integer.parseInt(totalMoney1);
                }
                String tuiNum1 = orderTotal.getTuiNum();//退款条数
                if (tuiNum1 != "null") {
                    tuiNum = tuiNum + Integer.parseInt(tuiNum1);
                }
                String settleAccounts1 = orderTotal.getSettleAccounts();
                if (settleAccounts1 != "null") {
                    settleAccounts = settleAccounts + Integer.parseInt(settleAccounts1);
                }
            }
            OrderTotal orderTotal = new OrderTotal();
            orderTotal.setSettleAccounts(String.valueOf(settleAccounts));
            orderTotal.setTotalMoney(String.valueOf(totalMoney));
            orderTotal.setTotal(String.valueOf(total));
            orderTotal.setCreateTime(startTime);
            orderTotal.setTuiMoney(String.valueOf(tuiMoney));
            orderTotal.setTuiNum(String.valueOf(tuiNum));
            orderTotal.setEmployyId(employee1.getId());
            orderTotalRepository.save(orderTotal);

        }

    }

    //员工汇总
    public void CountEmployee() {
        LOGGER.info("凌晨1点CountEmployee方法"+new Date());
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String format = sdf.format(time);
        String startTime = sdf.format(time) + " 00:00:00";
        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //查询所有员工id
        List<Employee> all = employeeRepository.findAll();
        for (Employee employee : all) {
            if (employee.getId() != null) {
                String employeeId = employee.getId();
                map.put("employeeId", employeeId);
                //按时间汇总
                Map mapConsumeWx = orderTotalMapper.eConsumeCountWx(map);//订单总金额和条数
                Map mapActualWx = orderTotalMapper.eActualCountWx(map);//实收总金额 和条数
                Map mapTuiWx = orderTotalMapper.eTuiCountWx(map);//退款总金额 和条数
                Map mapDiscountsWx = orderTotalMapper.eDiscountsCountWx(map); //折扣总金额
                OrderTotal orderTotalWx = new OrderTotal();
                orderTotalWx.setEmployyId(employee.getId());//门店id
                orderTotalWx.setPayWay(1);
                if (mapTuiWx != null) {
                    if (String.valueOf(mapTuiWx.get("totalTui")) == "null") {
                        orderTotalWx.setTuiNum("0");
                    } else {
                        orderTotalWx.setTuiNum(String.valueOf(mapTuiWx.get("totalTui")));//退款条数
                    }
                    if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null") {
                        orderTotalWx.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));//退款金额
                    }
                }
                if (mapActualWx != null) {
                    if (String.valueOf(mapActualWx.get("totalActual")) == "null") {
                        orderTotalWx.setTotal("0");
                    } else {
                        orderTotalWx.setTotal(String.valueOf(mapActualWx.get("totalActual")));//总条数
                    }
                    if (String.valueOf(mapActualWx.get("actualMoney")) == "null") {
                        orderTotalWx.setTotalMoney("0");
                    } else {
                        orderTotalWx.setTotalMoney(String.valueOf(mapActualWx.get("actualMoney")));//实收金额
                    }
                }
                if (mapDiscountsWx != null) {
                    if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null") {
                        orderTotalWx.setDiscountsMoney("0");
                    } else {
                        orderTotalWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));//折扣金额
                    }
                }
                if (mapConsumeWx != null) {
                    if (String.valueOf(mapConsumeWx.get("consumeMoney")) == "null") {
                        orderTotalWx.setConsumeMoney("0");
                    } else {
                        orderTotalWx.setConsumeMoney(String.valueOf(mapConsumeWx.get("consumeMoney")));//订单总金额
                    }
                }
                String actualMoneyWx = "0";
                String discountsMoneyWx = "0";
                if (mapActualWx != null) {
                    actualMoneyWx = String.valueOf(mapActualWx.get("actualMoney"));//实收金额
                }
                if (mapTuiWx != null) {
                    discountsMoneyWx = String.valueOf(mapTuiWx.get("tuiMoney"));//退款金额
                }
                Integer settleAccountsWx = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccountsWx = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccountsWx = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);//实收金额
                    settleAccountsWx = actualMoney + discountsMoney;
                }
                orderTotalWx.setSettleAccounts(String.valueOf(settleAccountsWx));

                orderTotalWx.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalWx);//保存到ordertotal表中


                //按支付宝时间汇总
                Map mapConsumeAlipay = orderTotalMapper.eConsumeCountAlipay(map);//该门店的订单总金额和条数
                Map mapActualAlipay = orderTotalMapper.eActualCountAlipay(map);//实收总金额 和条数
                Map mapTuiAlipay = orderTotalMapper.eTuiCountAlipay(map);//退款总金额 和条数
                Map mapDiscountsAlipay = orderTotalMapper.eDiscountsCountAlipay(map); //折扣总金额
                OrderTotal orderTotalAlipay = new OrderTotal();
                orderTotalAlipay.setEmployyId(employee.getId());//员工id
                orderTotalAlipay.setPayWay(0);
                if (mapTuiAlipay != null) {
                    if (String.valueOf(mapTuiAlipay.get("totalTui")) == "null") {
                        orderTotalAlipay.setTuiNum("0");
                    } else {
                        orderTotalAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("totalTui")));//退款条数
                    }
                    if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null") {
                        orderTotalAlipay.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                    }
                }
                if (mapActualAlipay != null) {
                    if (String.valueOf(mapActualAlipay.get("totalActual")) == "null") {
                        orderTotalAlipay.setTotal("0");
                    } else {
                        orderTotalAlipay.setTotal(String.valueOf(mapActualAlipay.get("totalActual")));//总条数
                    }
                    if (String.valueOf(mapActualAlipay.get("actualMoney")) == "null") {
                        orderTotalAlipay.setTotalMoney("0");
                    } else {
                        orderTotalAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("actualMoney")));//实收金额
                    }
                }
                if (mapDiscountsAlipay != null) {
                    if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null") {
                        orderTotalAlipay.setDiscountsMoney("0");
                    } else {
                        orderTotalAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                    }
                }
                if (mapConsumeAlipay != null) {
                    if (String.valueOf(mapConsumeAlipay.get("consumeMoney")) == "null") {
                        orderTotalAlipay.setConsumeMoney("0");
                    } else {
                        orderTotalAlipay.setConsumeMoney(String.valueOf(mapConsumeAlipay.get("consumeMoney")));//订单总金额
                    }
                }
                String actualMoney1 = "0";
                String discountsMoney1 = "0";
                if (mapActualAlipay != null) {
                    actualMoney1 = String.valueOf(mapActualAlipay.get("actualMoney"));//实收金额
                }
                if (mapTuiAlipay != null) {
                    discountsMoney1 = String.valueOf(mapTuiAlipay.get("tuiMoney"));//退款金额
                }
                Integer settleAccounts = 0;//订单金额
                if (actualMoney1 == "null" && discountsMoney1 == "null") {
                    settleAccounts = 0;
                }
                if (actualMoney1 == "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    settleAccounts = discountsMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 == "null") {
                    int actualMoney = Integer.parseInt(actualMoney1);//实收金额
                    settleAccounts = actualMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    int actualMoney = Integer.parseInt(actualMoney1);//实收金额
                    settleAccounts = actualMoney + discountsMoney;
                }
                orderTotalAlipay.setSettleAccounts(String.valueOf(settleAccounts));

                orderTotalAlipay.setCreateTime(format);//昨天的日期
                orderTotalRepository.save(orderTotalAlipay);//保存到ordertotal表中

                //微信+支付宝的
                OrderTotal orderTotal = new OrderTotal();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalAlipay.getTuiMoney() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiMoney());
                }
                if (orderTotalWx.getTuiMoney() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalAlipay.getTuiNum() == "null") {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalAlipay.getTuiNum());
                }
                if (orderTotalWx.getTuiNum() == "null") {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalAlipay.getTotal() == "null") {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalAlipay.getTotal());
                }
                if (orderTotalWx.getTotal() == "null") {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalAlipay.getTotalMoney() == "null") {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalAlipay.getTotalMoney());
                }
                if (orderTotalWx.getTotalMoney() == "null") {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalAlipay.getDiscountsMoney() == "null") {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalAlipay.getDiscountsMoney());
                }
                if (orderTotalWx.getDiscountsMoney() == "null") {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalAlipay.getSettleAccounts() == "null") {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalAlipay.getSettleAccounts());
                }
                if (orderTotalWx.getSettleAccounts() == "null") {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalAlipay.getConsumeMoney() == "null") {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalAlipay.getConsumeMoney());
                }
                if (orderTotalWx.getConsumeMoney() == "null") {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalWx.getConsumeMoney());
                }
                orderTotal.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotal.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotal.setCreateTime(format);
                orderTotal.setEmployyId(employee.getId());
                orderTotal.setTotal(String.valueOf(totalA + totalW));
                orderTotal.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotal.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotal.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotal.setPayWay(2);
                orderTotalRepository.save(orderTotal);//商户的微信支付宝日汇总保存到ordertotal表中

            }
        }

    }


    //代理商汇总
    public void CountAgent() {
        LOGGER.info("凌晨1点CountAgent方法"+new Date());
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String format = sdf.format(time);

        String startTime = sdf.format(time) + " 00:00:00";
        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        //商户的变量
        double discountsMoneyWx = 0;//折扣金额
        double totalMoneyWx = 0;//实收金额
        double tuiMoneyWx = 0;//退款金额
        double tuiNumWx = 0;//退款条数
        double totalWx = 0;//总条数
        double accountsWx = 0;//订单金额
        double consumeMoneyWx = 0;//消费金额

        double discountsMoneyAlipay = 0;//折扣金额
        double totalMoneyAlipay = 0;//实收金额
        double tuiMoneyAlipay = 0;//退款金额
        double tuiNumAlipay = 0;//退款条数
        double totalAlipay = 0;//总条数
        double accountsAlipay = 0;//订单金额
        double consumeMoneyAlipay = 0;//消费金额
        //查询所有代理商id
        List<Agent> all = agentRepository.findAll();
        for (Agent agent : all) {
            String agentId = agent.getId();
            map.put("agentId", agentId);
            List<String> list = new ArrayList();//保存该代理商的所有下级代理商的id

            List<Agent> byParentId = agentMapper.findByParentId(agentId);//该代理商的所有的
            if (byParentId == null) {//该代理是最下级的，只需将商户的汇总在一起
                //代理商下的商户的微信汇总
                OrderTotal orderTotalCWx = this.AgentCommercialWx(startTime, agentId, map);
                discountsMoneyWx = discountsMoneyWx + Double.parseDouble(orderTotalCWx.getDiscountsMoney());
                totalMoneyWx = totalMoneyWx + Double.parseDouble(orderTotalCWx.getTotalMoney());
                tuiMoneyWx = tuiMoneyWx + Double.parseDouble(orderTotalCWx.getTuiMoney());
                tuiNumWx = tuiNumWx + Double.parseDouble(orderTotalCWx.getTuiNum());
                totalWx = totalWx + Double.parseDouble(orderTotalCWx.getTotal());
                accountsWx = accountsWx + Double.parseDouble(orderTotalCWx.getSettleAccounts());
                consumeMoneyWx = consumeMoneyWx + Double.parseDouble(orderTotalCWx.getConsumeMoney());
                orderTotalCWx.setPayWay(1);
                orderTotalCWx.setCreateTime(format);
                orderTotalRepository.save(orderTotalCWx);//商户微信保存到ordertotal表中
                //代理商下的商户的支付宝汇总
                OrderTotal orderTotalCAlipay = this.AgentCommercialAlipay(startTime, agentId, map);
                discountsMoneyAlipay = discountsMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getDiscountsMoney());
                totalMoneyAlipay = totalMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getTotalMoney());
                tuiMoneyAlipay = tuiMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getTuiMoney());
                tuiNumAlipay = tuiNumAlipay + Double.parseDouble(orderTotalCAlipay.getTuiNum());
                totalAlipay = totalAlipay + Double.parseDouble(orderTotalCAlipay.getTotal());
                accountsAlipay = accountsAlipay + Double.parseDouble(orderTotalCAlipay.getSettleAccounts());
                consumeMoneyAlipay = consumeMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getConsumeMoney());
//                orderTotalRepository.save(orderTotalCAlipay);//商户支付宝保存到ordertotal表中
                //代理商下的商户微信和支付宝的日汇总
                OrderTotal orderTotal = new OrderTotal();
                orderTotal.setTuiNum(String.valueOf(tuiNumWx + tuiNumAlipay));
                orderTotal.setTuiMoney(String.valueOf(tuiMoneyWx + tuiMoneyAlipay));
                orderTotal.setCreateTime(format);
                orderTotal.setAgentId(agentId);
                orderTotal.setTotal(String.valueOf(totalAlipay + totalWx));
                orderTotal.setTotalMoney(String.valueOf(totalMoneyAlipay + totalMoneyWx));
                orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyAlipay + discountsMoneyWx));
                orderTotal.setSettleAccounts(String.valueOf(accountsAlipay + accountsWx));
                orderTotal.setConsumeMoney(String.valueOf(consumeMoneyAlipay + consumeMoneyWx));
                orderTotal.setPayWay(2);
                orderTotalRepository.save(orderTotal);//商户的微信支付宝日汇总保存到ordertotal表中
            }
            if (byParentId != null) {//不是最低层的代理，需要将下级代理和商户汇总在一起
                for (Agent agent1 : byParentId) {
                    String id = agent1.getId();
                    list.add(id);//添加下级代理商id
                }
                map.put("list", list);
                if (agentId != null) {
                    double discountsMoneyWxA = 0;//折扣金额
                    double totalMoneyWxA = 0;//实收金额
                    double tuiMoneyWxA = 0;//退款金额
                    double tuiNumWxA = 0;//退款条数
                    double totalWxA = 0;//总条数
                    double accountsWxA = 0;//订单金额
                    double consumeMoneyWxA = 0;//消费金额

                    double discountsMoneyAlipayA = 0;//折扣金额
                    double totalMoneyAlipayA = 0;//实收金额
                    double tuiMoneyAlipayA = 0;//退款金额
                    double tuiNumAlipayA = 0;//退款条数
                    double totalAlipayA = 0;//总条数
                    double accountsAlipayA = 0;//订单金额
                    double consumeMoneyAlipayA = 0;//消费金额
                    //代理商下的商户的微信汇总
                    OrderTotal orderTotalCWx = this.AgentCommercialWx(startTime, agentId, map);
                    discountsMoneyWx = discountsMoneyWx + Double.parseDouble(orderTotalCWx.getDiscountsMoney());
                    totalMoneyWx = totalMoneyWx + Double.parseDouble(orderTotalCWx.getTotalMoney());
                    tuiMoneyWx = tuiMoneyWx + Double.parseDouble(orderTotalCWx.getTuiMoney());
                    tuiNumWx = tuiNumWx + Double.parseDouble(orderTotalCWx.getTuiNum());
                    totalWx = totalWx + Double.parseDouble(orderTotalCWx.getTotal());
                    accountsWx = accountsWx + Double.parseDouble(orderTotalCWx.getSettleAccounts());
                    consumeMoneyWx = consumeMoneyWx + Double.parseDouble(orderTotalCWx.getConsumeMoney());
                    orderTotalCWx.setCreateTime(format);
                    orderTotalRepository.save(orderTotalCWx);//保存到ordertotal表中
                    //代理商下的商户的支付宝汇总
                    OrderTotal orderTotalCAlipay = this.AgentCommercialAlipay(startTime, agentId, map);
                    discountsMoneyAlipay = discountsMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getDiscountsMoney());
                    totalMoneyAlipay = totalMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getTotalMoney());
                    tuiMoneyAlipay = tuiMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getTuiMoney());
                    tuiNumAlipay = tuiNumAlipay + Double.parseDouble(orderTotalCAlipay.getTuiNum());
                    totalAlipay = totalAlipay + Double.parseDouble(orderTotalCAlipay.getTotal());
                    accountsAlipay = accountsAlipay + Double.parseDouble(orderTotalCAlipay.getSettleAccounts());
                    consumeMoneyAlipay = consumeMoneyAlipay + Double.parseDouble(orderTotalCAlipay.getConsumeMoney());
                    orderTotalCAlipay.setCreateTime(format);
                    orderTotalRepository.save(orderTotalCAlipay);
                    //代理商下的代理商的微信汇总
                    OrderTotal orderTotalAWx = this.AgentAgentWx(startTime, agentId, map);
                    discountsMoneyWxA = discountsMoneyWxA + Double.parseDouble(orderTotalAWx.getDiscountsMoney());
                    totalMoneyWxA = totalMoneyWxA + Double.parseDouble(orderTotalAWx.getTotalMoney());
                    tuiMoneyWxA = tuiMoneyWxA + Double.parseDouble(orderTotalAWx.getTuiMoney());
                    tuiNumWxA = tuiNumWxA + Double.parseDouble(orderTotalAWx.getTuiNum());
                    totalWxA = totalWxA + Double.parseDouble(orderTotalAWx.getTotal());
                    accountsWxA = accountsWxA + Double.parseDouble(orderTotalAWx.getSettleAccounts());
                    consumeMoneyWxA = consumeMoneyWxA + Double.parseDouble(orderTotalAWx.getConsumeMoney());
                    //代理商下的代理商的支付宝汇总
                    OrderTotal orderTotalAlipay = this.AgentAgentAlipay(startTime, agentId, map);
                    discountsMoneyAlipayA = discountsMoneyAlipayA + Double.parseDouble(orderTotalAlipay.getDiscountsMoney());
                    totalMoneyAlipayA = totalMoneyAlipayA + Double.parseDouble(orderTotalAlipay.getTotalMoney());
                    tuiMoneyAlipayA = tuiMoneyAlipayA + Double.parseDouble(orderTotalAlipay.getTuiMoney());
                    tuiNumAlipayA = tuiNumAlipayA + Double.parseDouble(orderTotalAlipay.getTuiNum());
                    totalAlipayA = totalAlipayA + Double.parseDouble(orderTotalAlipay.getTotal());
                    accountsAlipayA = accountsAlipayA + Double.parseDouble(orderTotalAlipay.getSettleAccounts());
                    consumeMoneyAlipayA = consumeMoneyAlipayA + Double.parseDouble(orderTotalAlipay.getConsumeMoney());
                    //下级代理和商户的微信日汇总
                    OrderTotal orderTotal = new OrderTotal();
                    orderTotal.setConsumeMoney(String.valueOf(consumeMoneyWxA + consumeMoneyWx));
                    orderTotal.setSettleAccounts(String.valueOf(accountsWxA + accountsWx));
                    orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyWxA + discountsMoneyWx));
                    orderTotal.setTotalMoney(String.valueOf(totalMoneyWx + totalWxA));
                    orderTotal.setTotal(String.valueOf(totalWxA + totalWx));
                    orderTotal.setCreateTime(format);
                    orderTotal.setPayWay(1);
                    orderTotal.setTuiMoney(String.valueOf(tuiMoneyWxA + tuiMoneyWx));
                    orderTotal.setTuiNum(String.valueOf(tuiNumWx + tuiNumWxA));
                    orderTotal.setAgentId(agentId);
                    orderTotalRepository.save(orderTotal);
                    //下级代理商和商户的支付宝日汇总
                    orderTotal.setConsumeMoney(String.valueOf(consumeMoneyAlipayA + consumeMoneyAlipay));
                    orderTotal.setSettleAccounts(String.valueOf(accountsAlipayA + accountsAlipay));
                    orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyAlipayA + discountsMoneyAlipay));
                    orderTotal.setTotalMoney(String.valueOf(totalMoneyAlipayA + totalAlipay));
                    orderTotal.setTotal(String.valueOf(totalAlipayA + totalAlipay));
                    orderTotal.setCreateTime(format);
                    orderTotal.setPayWay(0);
                    orderTotal.setTuiMoney(String.valueOf(tuiMoneyAlipayA + tuiMoneyAlipay));
                    orderTotal.setTuiNum(String.valueOf(tuiNumAlipayA + tuiNumAlipayA));
                    orderTotal.setAgentId(agentId);
                    orderTotalRepository.save(orderTotal);

                    //下级代理和商户的支付宝+微信的日汇总
                    orderTotal.setConsumeMoney(String.valueOf(consumeMoneyAlipayA + consumeMoneyAlipay + consumeMoneyWx + consumeMoneyWxA));
                    orderTotal.setSettleAccounts(String.valueOf(accountsAlipayA + accountsAlipay + accountsWx + accountsWxA));
                    orderTotal.setDiscountsMoney(String.valueOf(discountsMoneyAlipayA + discountsMoneyAlipay + discountsMoneyWx + discountsMoneyWxA));
                    orderTotal.setTotalMoney(String.valueOf(totalMoneyAlipayA + totalAlipay + totalMoneyWx + totalWxA));
                    orderTotal.setTotal(String.valueOf(totalAlipayA + totalAlipay + totalWx + totalWxA));
                    orderTotal.setCreateTime(format);
                    orderTotal.setTuiMoney(String.valueOf(tuiMoneyAlipayA + tuiMoneyAlipay + tuiMoneyWx + tuiMoneyWxA));
                    orderTotal.setTuiNum(String.valueOf(tuiNumAlipayA + tuiNumAlipayA + tuiNumWx + tuiNumWxA));
                    orderTotal.setAgentId(agentId);
                    orderTotal.setPayWay(2);
                    orderTotalRepository.save(orderTotal);
                }
            }
        }
    }

    //代理商下的代理商的微信汇总
    public OrderTotal AgentAgentWx(String startTime, String agentId, Map map) {

        Map mapConsumeCWx = orderTotalMapper.consumeAWx(map);//该子代理商的订单总金额和条数
        Map mapDiscountsCWx = orderTotalMapper.discountsAWx(map); //折扣总金额
        Map mapTuiCWx = orderTotalMapper.tuiAWx(map);//退款总金额 和条数
        Map mapActualCWx = orderTotalMapper.actualAWx(map);//实收总金额和条数

        OrderTotal orderTotalCWx = new OrderTotal();
        orderTotalCWx.setAgentId(agentId);//代理商id
        orderTotalCWx.setCreateTime(startTime);//昨天的日期
        orderTotalCWx.setPayWay(1);
        if (mapTuiCWx != null) {
            if (String.valueOf(mapTuiCWx.get("tuiNum")) == "null") {
                orderTotalCWx.setTuiNum("0");
            } else {
                orderTotalCWx.setTuiNum(String.valueOf(mapTuiCWx.get("tuiNum")));//退款条数
            }
            if (String.valueOf(mapTuiCWx.get("tuiMoney")) == "null") {
                orderTotalCWx.setTuiMoney("0");
            } else {
                orderTotalCWx.setTuiMoney(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
            }
        } else {
            orderTotalCWx.setTuiNum("0");
            orderTotalCWx.setTuiMoney("0");
            orderTotalCWx.setTuiMoney("0");
        }
        if (mapConsumeCWx != null) {
            if (String.valueOf(mapDiscountsCWx.get("discountsMoney")) == "null") {
                orderTotalCWx.setDiscountsMoney("0");//折扣金额
            } else {
                orderTotalCWx.setDiscountsMoney(String.valueOf(mapDiscountsCWx.get("discountsMoney")));//折扣金额
            }
        } else {
            orderTotalCWx.setDiscountsMoney("0");//折扣金额
        }
        if (mapDiscountsCWx != null) {
            if (String.valueOf(mapConsumeCWx.get("consumeMoney")) == "null") {
                orderTotalCWx.setConsumeMoney("0");
            } else {
                orderTotalCWx.setConsumeMoney(String.valueOf(mapConsumeCWx.get("consumeMoney")));//订单总金额
            }
        } else {
            orderTotalCWx.setConsumeMoney("0");
        }
        if (mapActualCWx != null) {
            if (String.valueOf(mapActualCWx.get("total")) == "null") {
                orderTotalCWx.setTotal("0");
            } else {
                orderTotalCWx.setTotal(String.valueOf(mapActualCWx.get("total")));//总条数
            }
            if (String.valueOf(mapActualCWx.get("totalMoney")) == "null") {
                orderTotalCWx.setTotalMoney("0");
            } else {
                orderTotalCWx.setTotalMoney(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
            }

        } else {
            orderTotalCWx.setTotal("0");
            orderTotalCWx.setTotalMoney("0");
        }
        String actualMoney1 = "0";
        String discountsMoney1 = "0";
        if (mapActualCWx != null && mapActualCWx.get("totalMoney") != "0" && String.valueOf(mapActualCWx.get("totalMoney")) != "null") {
            actualMoney1 = String.valueOf(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
        }
        if (mapTuiCWx != null && String.valueOf(mapTuiCWx.get("tuiMoney")) != "0" && String.valueOf(mapTuiCWx.get("tuiMoney")) != "null") {
            discountsMoney1 = String.valueOf(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
        }

        Integer settleAccounts = 0;//订单金额
        if (actualMoney1 == "0" && discountsMoney1 == "0") {//实收金额和退款为空
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 == "0" && discountsMoney1 != "0") {//实收金额为空，退款金额不为空
            int discountsMoney = Integer.parseInt(discountsMoney1);
            settleAccounts = discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 == "0") {//实收金额不为空，退款金额为空
            int actualMoney = Integer.parseInt(actualMoney1);//实收金额
            settleAccounts = actualMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 != "0") {//实收金额和退款金额都不为空
            Double discountsMoney = Double.parseDouble(discountsMoney1);
            Double actualMoney = (Double.parseDouble(actualMoney1));//实收金额
            double settleAccounts1 = actualMoney + discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts1));
        }
        return orderTotalCWx;
    }

    //按代理商下的代理商的支付宝汇总
    public OrderTotal AgentAgentAlipay(String startTime, String agentId, Map map) {

        Map mapConsumeCWx = orderTotalMapper.consumeAAlipay(map);//该门店的订单总金额和条数
        Map mapDiscountsCWx = orderTotalMapper.discountsAAlipay(map); //折扣总金额
        Map mapTuiCWx = orderTotalMapper.tuiAAlipay(map);//退款总金额 和条数
        Map mapActualCWx = orderTotalMapper.actualAAlipay(map);//实收总金额 和条数


        OrderTotal orderTotalCWx = new OrderTotal();
        orderTotalCWx.setAgentId(agentId);//代理商id
        orderTotalCWx.setCreateTime(startTime);//昨天的日期
        orderTotalCWx.setPayWay(0);
        if (mapTuiCWx != null) {
            if (String.valueOf(mapTuiCWx.get("tuiNum")) == "null") {
                orderTotalCWx.setTuiNum("0");
            } else {
                orderTotalCWx.setTuiNum(String.valueOf(mapTuiCWx.get("tuiNum")));//退款条数
            }
            if (String.valueOf(mapTuiCWx.get("tuiMoney")) == "null") {
                orderTotalCWx.setTuiMoney("0");
            } else {
                orderTotalCWx.setTuiMoney(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
            }
        } else {
            orderTotalCWx.setTuiNum("0");
            orderTotalCWx.setTuiMoney("0");
            orderTotalCWx.setTuiMoney("0");
        }
        if (mapConsumeCWx != null) {
            if (String.valueOf(mapDiscountsCWx.get("discountsMoney")) == "null") {
                orderTotalCWx.setDiscountsMoney("0");//折扣金额
            } else {
                orderTotalCWx.setDiscountsMoney(String.valueOf(mapDiscountsCWx.get("discountsMoney")));//折扣金额
            }
        } else {
            orderTotalCWx.setDiscountsMoney("0");//折扣金额
        }
        if (mapDiscountsCWx != null) {
            if (String.valueOf(mapConsumeCWx.get("consumeMoney")) == "null") {
                orderTotalCWx.setConsumeMoney("0");
            } else {
                orderTotalCWx.setConsumeMoney(String.valueOf(mapConsumeCWx.get("consumeMoney")));//订单总金额
            }
        } else {
            orderTotalCWx.setConsumeMoney("0");
        }
        if (mapActualCWx != null) {
            if (String.valueOf(mapActualCWx.get("total")) == "null") {
                orderTotalCWx.setTotal("0");
            } else {
                orderTotalCWx.setTotal(String.valueOf(mapActualCWx.get("total")));//总条数
            }
            if (String.valueOf(mapActualCWx.get("totalMoney")) == "null") {
                orderTotalCWx.setTotalMoney("0");
            } else {
                orderTotalCWx.setTotalMoney(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
            }

        } else {
            orderTotalCWx.setTotal("0");
            orderTotalCWx.setTotalMoney("0");
        }
        String actualMoney1 = "0";
        String discountsMoney1 = "0";
        if (mapActualCWx != null && mapActualCWx.get("totalMoney") != "0" && String.valueOf(mapActualCWx.get("totalMoney")) != "null") {
            actualMoney1 = String.valueOf(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
        }
        if (mapTuiCWx != null && String.valueOf(mapTuiCWx.get("tuiMoney")) != "0" && String.valueOf(mapTuiCWx.get("tuiMoney")) != "null") {
            discountsMoney1 = String.valueOf(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
        }

        Integer settleAccounts = 0;//订单金额
        if (actualMoney1 == "0" && discountsMoney1 == "0") {//实收金额和退款为空
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 == "0" && discountsMoney1 != "0") {//实收金额为空，退款金额不为空
            int discountsMoney = Integer.parseInt(discountsMoney1);
            settleAccounts = discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 == "0") {//实收金额不为空，退款金额为空
            int actualMoney = Integer.parseInt(actualMoney1);//实收金额
            settleAccounts = actualMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 != "0") {//实收金额和退款金额都不为空
            Double discountsMoney = Double.parseDouble(discountsMoney1);
            Double actualMoney = (Double.parseDouble(actualMoney1));//实收金额
            double settleAccounts1 = actualMoney + discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts1));
        }
        return orderTotalCWx;
    }

    //按代理商下的商户的支付宝汇总
    public OrderTotal AgentCommercialAlipay(String startTime, String agentId, Map map) {

        Map mapConsumeCAlipay = orderTotalMapper.consumeCAlipay(map);//该门店的订单总金额和条数
        Map mapDiscountsCAlipay = orderTotalMapper.discountsCAlipay(map); //折扣总金额
        Map mapTuiCAlipay = orderTotalMapper.tuiCAlipay(map);//退款总金额 和条数
        Map mapActualCAlipay = orderTotalMapper.actualCAlipay(map);//实收总金额 和条数
        OrderTotal orderTotalCAlipay = new OrderTotal();
        orderTotalCAlipay.setAgentId(agentId);//代理商id
        orderTotalCAlipay.setCreateTime(startTime);//昨天的日期
        orderTotalCAlipay.setPayWay(0);
        if (mapTuiCAlipay != null) {
            if (String.valueOf(mapTuiCAlipay.get("tuiNum")) == "null") {
                orderTotalCAlipay.setTuiNum("0");
            } else {
                orderTotalCAlipay.setTuiNum(String.valueOf(mapTuiCAlipay.get("tuiNum")));//退款条数
            }
            if (String.valueOf(mapTuiCAlipay.get("tuiMoney")) == "null") {
                orderTotalCAlipay.setTuiMoney("0");
            } else {
                orderTotalCAlipay.setTuiMoney(String.valueOf(mapTuiCAlipay.get("tuiMoney")));//退款金额
            }
        } else {
            orderTotalCAlipay.setTuiNum("0");
            orderTotalCAlipay.setTuiMoney("0");
            orderTotalCAlipay.setTuiMoney("0");
        }
        if (mapConsumeCAlipay != null) {
            if (String.valueOf(mapDiscountsCAlipay.get("discountsMoney")) == "null") {
                orderTotalCAlipay.setDiscountsMoney("0");//折扣金额
            } else {
                orderTotalCAlipay.setDiscountsMoney(String.valueOf(mapDiscountsCAlipay.get("discountsMoney")));//折扣金额
            }
        } else {
            orderTotalCAlipay.setDiscountsMoney("0");//折扣金额
        }
        if (mapDiscountsCAlipay != null) {
            if (String.valueOf(mapConsumeCAlipay.get("consumeMoney")) == "null") {
                orderTotalCAlipay.setConsumeMoney("0");
            } else {
                orderTotalCAlipay.setConsumeMoney(String.valueOf(mapConsumeCAlipay.get("consumeMoney")));//订单总金额
            }
        } else {
            orderTotalCAlipay.setConsumeMoney("0");
        }
        if (mapActualCAlipay != null) {
            if (String.valueOf(mapActualCAlipay.get("total")) == "null") {
                orderTotalCAlipay.setTotal("0");
            } else {
                orderTotalCAlipay.setTotal(String.valueOf(mapActualCAlipay.get("total")));//总条数
            }
            if (String.valueOf(mapActualCAlipay.get("totalMoney")) == "null") {
                orderTotalCAlipay.setTotalMoney("0");
            } else {
                orderTotalCAlipay.setTotalMoney(String.valueOf(mapActualCAlipay.get("totalMoney")));//实收金额
            }

        } else {
            orderTotalCAlipay.setTotal("0");
            orderTotalCAlipay.setTotalMoney("0");
        }
        String actualMoney1 = "0";
        String discountsMoney1 = "0";
        if (mapActualCAlipay != null && mapActualCAlipay.get("totalMoney") != "0" && String.valueOf(mapActualCAlipay.get("totalMoney")) != "null") {
            actualMoney1 = String.valueOf(String.valueOf(mapActualCAlipay.get("totalMoney")));//实收金额
        }
        if (mapTuiCAlipay != null && String.valueOf(mapTuiCAlipay.get("tuiMoney")) != "0" && String.valueOf(mapTuiCAlipay.get("tuiMoney")) != "null") {
            discountsMoney1 = String.valueOf(String.valueOf(mapTuiCAlipay.get("tuiMoney")));//退款金额
        }

        Integer settleAccounts = 0;//订单金额
        if (actualMoney1 == "0" && discountsMoney1 == "0") {//实收金额和退款为空
            orderTotalCAlipay.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 == "0" && discountsMoney1 != "0") {//实收金额为空，退款金额不为空
            int discountsMoney = Integer.parseInt(discountsMoney1);
            settleAccounts = discountsMoney;
            orderTotalCAlipay.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 == "0") {//实收金额不为空，退款金额为空
            int actualMoney = Integer.parseInt(actualMoney1);//实收金额
            settleAccounts = actualMoney;
            orderTotalCAlipay.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 != "0") {//实收金额和退款金额都不为空
            Double discountsMoney = Double.parseDouble(discountsMoney1);
            Double actualMoney = (Double.parseDouble(actualMoney1));//实收金额
            double settleAccounts1 = actualMoney + discountsMoney;
            orderTotalCAlipay.setSettleAccounts(String.valueOf(settleAccounts1));
        }
        return orderTotalCAlipay;
    }

    //按代理商下的商户的微信汇总
    public OrderTotal AgentCommercialWx(String startTime, String agentId, Map map) {

        Map mapConsumeCWx = orderTotalMapper.consumeCWx(map);//该门店的订单总金额和条数
        Map mapDiscountsCWx = orderTotalMapper.discountsCWx(map); //折扣总金额
        Map mapTuiCWx = orderTotalMapper.tuiCWx(map);//退款总金额 和条数
        Map mapActualCWx = orderTotalMapper.actualCWx(map);//实收总金额 和条数
        OrderTotal orderTotalCWx = new OrderTotal();
        orderTotalCWx.setAgentId(agentId);//代理商id
        orderTotalCWx.setCreateTime(startTime);//昨天的日期
        orderTotalCWx.setPayWay(1);
        if (mapTuiCWx != null) {
            if (String.valueOf(mapTuiCWx.get("tuiNum")) == "null") {
                orderTotalCWx.setTuiNum("0");
            } else {
                orderTotalCWx.setTuiNum(String.valueOf(mapTuiCWx.get("tuiNum")));//退款条数
            }
            if (String.valueOf(mapTuiCWx.get("tuiMoney")) == "null") {
                orderTotalCWx.setTuiMoney("0");
            } else {
                orderTotalCWx.setTuiMoney(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
            }
        } else {
            orderTotalCWx.setTuiNum("0");
            orderTotalCWx.setTuiMoney("0");
            orderTotalCWx.setTuiMoney("0");
        }
        if (mapConsumeCWx != null) {
            if (String.valueOf(mapDiscountsCWx.get("discountsMoney")) == "null") {
                orderTotalCWx.setDiscountsMoney("0");//折扣金额
            } else {
                orderTotalCWx.setDiscountsMoney(String.valueOf(mapDiscountsCWx.get("discountsMoney")));//折扣金额
            }
        } else {
            orderTotalCWx.setDiscountsMoney("0");//折扣金额
        }
        if (mapDiscountsCWx != null) {
            if (String.valueOf(mapConsumeCWx.get("consumeMoney")) == "null") {
                orderTotalCWx.setConsumeMoney("0");
            } else {
                orderTotalCWx.setConsumeMoney(String.valueOf(mapConsumeCWx.get("consumeMoney")));//订单总金额
            }
        } else {
            orderTotalCWx.setConsumeMoney("0");
        }
        if (mapActualCWx != null) {
            if (String.valueOf(mapActualCWx.get("total")) == "null") {
                orderTotalCWx.setTotal("0");
            } else {
                orderTotalCWx.setTotal(String.valueOf(mapActualCWx.get("total")));//总条数
            }
            if (String.valueOf(mapActualCWx.get("totalMoney")) == "null") {
                orderTotalCWx.setTotalMoney("0");
            } else {
                orderTotalCWx.setTotalMoney(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
            }

        } else {
            orderTotalCWx.setTotal("0");
            orderTotalCWx.setTotalMoney("0");
        }
        String actualMoney1 = "0";
        String discountsMoney1 = "0";
        if (mapActualCWx != null && mapActualCWx.get("totalMoney") != "0" && String.valueOf(mapActualCWx.get("totalMoney")) != "null") {
            actualMoney1 = String.valueOf(String.valueOf(mapActualCWx.get("totalMoney")));//实收金额
        }
        if (mapTuiCWx != null && String.valueOf(mapTuiCWx.get("tuiMoney")) != "0" && String.valueOf(mapTuiCWx.get("tuiMoney")) != "null") {
            discountsMoney1 = String.valueOf(String.valueOf(mapTuiCWx.get("tuiMoney")));//退款金额
        }

        Integer settleAccounts = 0;//订单金额
        if (actualMoney1 == "0" && discountsMoney1 == "0") {//实收金额和退款为空
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 == "0" && discountsMoney1 != "0") {//实收金额为空，退款金额不为空
            int discountsMoney = Integer.parseInt(discountsMoney1);
            settleAccounts = discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 == "0") {//实收金额不为空，退款金额为空
            int actualMoney = Integer.parseInt(actualMoney1);//实收金额
            settleAccounts = actualMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts));
        }
        if (actualMoney1 != "0" && discountsMoney1 != "0") {//实收金额和退款金额都不为空
            Double discountsMoney = Double.parseDouble(discountsMoney1);
            Double actualMoney = (Double.parseDouble(actualMoney1));//实收金额
            double settleAccounts1 = actualMoney + discountsMoney;
            orderTotalCWx.setSettleAccounts(String.valueOf(settleAccounts1));
        }
        return orderTotalCWx;
    }

    //旧版的代理商汇总
    public void AgentCount() {
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        Date time = calendar.getTime();
        String startTime = sdf.format(time) + " 00:00:00";
        String endTime = sdf.format(time) + " 23:59:59";
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        //查询所有代理商id
        List<Agent> all = agentRepository.findAll();
        for (Agent agent : all) {
            String agentId = agent.getId();
            String parentId = agent.getParentId();
            map.put("agentId", agentId);
            if (agentId != null) {
                //按时间汇总
                Map mapConsume1 = orderTotalMapper.consumeCount1(map);//该门店的订单总金额和条数
                Map mapDiscounts1 = orderTotalMapper.discountsCount1(map); //折扣总金额
                Map mapTui1 = orderTotalMapper.tuiCount1(map);//退款总金额 和条数
                Map mapActual1 = orderTotalMapper.actualCount1(map);//实收总金额 和条数
                Map mapConsume = orderTotalMapper.consumeCount(agentId);//该门店的订单总金额和条数
                Map mapActual = orderTotalMapper.actualCount(agentId);//实收总金额 和条数
                Map mapTui = orderTotalMapper.tuiCount(agentId);//退款总金额 和条数
                Map mapDiscounts = orderTotalMapper.discountsCount(agentId); //折扣总金额
                OrderTotal orderTotal = new OrderTotal();
                orderTotal.setAgentId(agent.getId());//代理商id
//                orderTotal.setParentId(parentId);//父级id
                orderTotal.setCreateTime(startTime);//昨天的日期
                if (mapTui1 != null) {
                    if (String.valueOf(mapTui1.get("tuiNum")) == "null") {
                        orderTotal.setTuiNum("0");
                    } else {
                        orderTotal.setTuiNum(String.valueOf(mapTui1.get("tuiNum")));//退款条数
                    }
                    if (String.valueOf(mapTui1.get("tuiMoney")) == "null") {
                        orderTotal.setTuiMoney("0");
                    } else {
                        orderTotal.setTuiMoney(String.valueOf(mapTui1.get("tuiMoney")));//退款金额
                    }
                } else {
                    orderTotal.setTuiNum("0");
                    orderTotal.setTuiMoney("0");
                    orderTotal.setTuiMoney("0");
                }
                if (mapConsume1 != null) {
                    if (String.valueOf(mapDiscounts1.get("discountsMoney")) == "null") {
                        orderTotal.setDiscountsMoney("0");//折扣金额
                    } else {
                        orderTotal.setDiscountsMoney(String.valueOf(mapDiscounts1.get("discountsMoney")));//折扣金额
                    }
                } else {
                    orderTotal.setDiscountsMoney("0");//折扣金额
                }
                if (mapDiscounts1 != null) {
                    if (String.valueOf(mapConsume1.get("consumeMoney")) == "null") {
                        orderTotal.setConsumeMoney("0");
                    } else {
                        orderTotal.setConsumeMoney(String.valueOf(mapConsume1.get("consumeMoney")));//订单总金额
                    }
                } else {
                    orderTotal.setConsumeMoney("0");
                }
                if (mapActual1 != null) {
                    if (String.valueOf(mapActual1.get("total")) == "null") {
                        orderTotal.setTotal("0");
                    } else {
                        orderTotal.setTotal(String.valueOf(mapActual1.get("total")));//总条数
                    }
                    if (String.valueOf(mapActual1.get("totalMoney")) == "null") {
                        orderTotal.setTotalMoney("0");
                    } else {
                        orderTotal.setTotalMoney(String.valueOf(mapActual1.get("totalMoney")));//实收金额
                    }

                } else {
                    orderTotal.setTotal("0");
                    orderTotal.setTotalMoney("0");
                }
                String actualMoney1 = "0";
                String discountsMoney1 = "0";
                if (mapActual1 != null && mapActual1.get("totalMoney") != "0" && String.valueOf(mapActual1.get("totalMoney")) != "null") {
                    actualMoney1 = String.valueOf(String.valueOf(mapActual1.get("totalMoney")));//实收金额
                }
                if (mapTui1 != null && String.valueOf(mapTui1.get("tuiMoney")) != "0" && String.valueOf(mapTui1.get("tuiMoney")) != "null") {
                    discountsMoney1 = String.valueOf(String.valueOf(mapTui1.get("tuiMoney")));//退款金额
                }

                Integer settleAccounts = 0;//订单金额
                if (actualMoney1 == "0" && discountsMoney1 == "0") {//实收金额和退款为空
                    orderTotal.setSettleAccounts(String.valueOf(settleAccounts));
                }
                if (actualMoney1 == "0" && discountsMoney1 != "0") {//实收金额为空，退款金额不为空
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    settleAccounts = discountsMoney;
                    orderTotal.setSettleAccounts(String.valueOf(settleAccounts));
                }
                if (actualMoney1 != "0" && discountsMoney1 == "0") {//实收金额不为空，退款金额为空
                    int actualMoney = Integer.parseInt(actualMoney1);//实收金额
                    settleAccounts = actualMoney;
                    orderTotal.setSettleAccounts(String.valueOf(settleAccounts));
                }
                if (actualMoney1 != "0" && discountsMoney1 != "0") {//实收金额和退款金额都不为空
                    Double discountsMoney = Double.parseDouble(discountsMoney1);
                    Double actualMoney = (Double.parseDouble(actualMoney1));//实收金额
                    double settleAccounts1 = actualMoney + discountsMoney;
                    orderTotal.setSettleAccounts(String.valueOf(settleAccounts1));
                }

                orderTotalRepository.save(orderTotal);//保存到ordertotal表中
            }
        }
    }

    //汇总查询
    public QueryResponseResult queryCount() {
        OrderQueryCountRequest orderQueryGroupRequest = new OrderQueryCountRequest();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer type = user.getType();
        if (type == 1) {
            //代理商汇总查询
            Agent byUserId = agentRepository.findByUserId(user.getId());
            String id = byUserId.getId();
            orderQueryGroupRequest.setAgentId(id);//代理商id
        }
        if (type == 2) {
            //商户汇总查询
            CommercialTenant byUserId = commercialRepository.findByUserId(user.getId());
            orderQueryGroupRequest.setCId(byUserId.getId());//商户id
        }
        if (type == 4) {
            //员工汇总查询
            Employee byUserId = employeeRepository.findByUserId(user.getId());
            orderQueryGroupRequest.setEmployeeId(byUserId.getId());//商户id
        }
        List<Map> list = orderTotalMapper.queryCount(orderQueryGroupRequest);
        QueryResult queryResult = new QueryResult();
        queryResult.setTotal(list.size());
        queryResult.setList(list);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }


    //商户门店的汇总查询
    public QueryResponseResult shopCount(int page, int limit) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        CommercialTenant byUserId = commercialRepository.findByUserId(userId);
        String cId = byUserId.getId();
        List<Merchants> list = merchantsRepository.findByCommercialTenantId(cId);

        PageHelper.startPage(page, limit);
        List<Map> list1 = orderTotalMapper.shopCount(list);
        for (Map map : list1) {
            String shopId = String.valueOf(map.get("shopId"));
            Optional<Merchants> byId = merchantsRepository.findById(shopId);
            if (byId.isPresent()) {
                String shopName = byId.get().getName();
                map.put("shopName", shopName);
            }
        }
        PageInfo pageInfo = new PageInfo(list1);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(pageInfo.getList());
        queryResult.setTotal(pageInfo.getTotal());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //订单有效汇总
    /*
     * 1.得到登录者id
     * 2.管理员
     * 3.代理商
     * 4.商户
     * 5.员工
     * */
    public QueryResponseResult findListSelect(OrderQueryRequest orderQueryRequest) {
        LOGGER.info("订单有效汇总findListSelect方法："+new Date()+"，参数："+orderQueryRequest);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();//登录者的userID
        Integer type = user.getType();//登录者的 类型
        type = user.getType();
        /*if (type == 1) {//代理商，查询出下面的商户*/
        List<String> commercialTenantIdList = null;
        commercialTenantIdList = this.findCommercial(user.getId());
        if (type == 1) {
            //代理商，查询出下面的商户
            //查询出代理商id
            Agent agent = agentRepository.findByUserId(user.getId());
            commercialTenantIdList = this.findCommercial(agent.getId());
        }
        if (type == 2) {//商户
            CommercialTenant byUserId = commercialRepository.findByUserId(user.getId());
            if (byUserId == null) {
                ExceptionCast.cast(CommonCode.FAIL);
            }
            orderQueryRequest.setCommercialTenantId(byUserId.getId());
        }
        if (type == 3) {    //门店,依据sn号查询

            if (StringUtils.isEmpty(orderQueryRequest.getSn())) {
                ExceptionCast.cast(CommonCode.FAIL);
            }
        }
        if (type == 0) {    //管理员
            List<CommercialTenant> all = commercialRepository.findAll();
            for (CommercialTenant commercialTenant : all) {
                commercialTenantIdList.add(commercialTenant.getId());
            }
        }
        if (type == 4) { //员工查询
            List<CommercialTenant> list = commercialRepository.findByEmployeeId(user.getId());
            for (CommercialTenant commercialTenant : list) {
                commercialTenantIdList.add(commercialTenant.getId());
            }
        }

        if (type == 5) { //店员查询

            if (StringUtils.isEmpty(orderQueryRequest.getSn())) {
                ExceptionCast.cast(CommonCode.FAIL);
            }
        }
        if (orderQueryRequest == null) {
            new OrderQueryRequest();
        }
        Date startTime = null;
        Date endTime = null;
        if (orderQueryRequest.getDateTime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTimeStr = simpleDateFormat.format(orderQueryRequest.getDateTime());
            try {
                startTime = simpleDateFormat.parse(startTimeStr);
                long time = startTime.getTime();
                long endTimeLong = time + (24 * 60 * 60 * 1000);
                endTime = new Date(endTimeLong);
            } catch (ParseException e) {
                LOGGER.error("时间格式转换异常:{}", e.getMessage());
            }
        }
        Date finalStartTime = startTime;
        Date finalEndTime = endTime;

        Date finalEndTimes = orderQueryRequest.getEndTime();
        Date finalStartTimes = orderQueryRequest.getStartTime();

        List<String> finalCommercialTenantIdList = commercialTenantIdList;
        Integer finalType = type;
        Specification specification = (var1, var2, var3) -> {
            Predicate predicate = var3.conjunction();
            if (finalEndTimes != null && finalStartTimes != null) {
                //后台登录起始日期
                predicate.getExpressions().add(var3.greaterThanOrEqualTo(var1.get("createTime").as(Date.class), finalStartTimes));
                //结束日期
                predicate.getExpressions().add(var3.lessThanOrEqualTo(var1.get("createTime").as(Date.class), finalEndTimes));
            }
            if (StringUtils.isNotEmpty(orderQueryRequest.getSn())) {
                predicate.getExpressions().add(var3.equal(var1.get("sn"), orderQueryRequest.getSn()));
            }
            //支付类型
            if (StringUtils.isNotEmpty(orderQueryRequest.getPayType())) {
                predicate.getExpressions().add(var3.equal(var1.get("payType"), orderQueryRequest.getPayType()));
            }
            //支付状态
            if (orderQueryRequest.getStatus() != null) {
                predicate.getExpressions().add(var3.equal(var1.get("status"), orderQueryRequest.getStatus()));
            }
            //支付方式
            if (StringUtils.isNotEmpty(orderQueryRequest.getPayWay())) {
                predicate.getExpressions().add(var3.equal(var1.get("payWay"), orderQueryRequest.getPayWay()));
            }
            //订单号
            if (StringUtils.isNotEmpty(orderQueryRequest.getOutTradeNo())) {
                predicate.getExpressions().add(var3.equal(var1.get("outTradeNo"), orderQueryRequest.getOutTradeNo()));
            }
            //流水号

            if (StringUtils.isNotEmpty(orderQueryRequest.getSerialNumber())) {
                predicate.getExpressions().add(var3.equal(var1.get("serialNumber"), orderQueryRequest.getSerialNumber()));
            }
            //门店id

            if (StringUtils.isNotEmpty(orderQueryRequest.getShopId())) {
                predicate.getExpressions().add(var3.equal(var1.get("shopId"), orderQueryRequest.getShopId()));
            }
            if (finalEndTime != null && finalStartTime != null) {
                //起始日期
                predicate.getExpressions().add(var3.greaterThanOrEqualTo(var1.get("createTime").as(Date.class), finalStartTime));
                //结束日期
                predicate.getExpressions().add(var3.lessThanOrEqualTo(var1.get("createTime").as(Date.class), finalEndTime));
            }


            if (finalType == 1 || finalType == 0 || finalType == 4) {//代理商，管理员，员工
                //商家id条件
                CriteriaBuilder.In<String> in = var3.in(var1.get("commercialTenantId"));
                if (finalCommercialTenantIdList != null) {
                    for (String s : finalCommercialTenantIdList) {
                        in.value(s);
                    }
                } else {
                    in.value("0");
                }
                predicate.getExpressions().add(in);
            }

            if (finalType == 2) {//商户
                predicate.getExpressions().add(var3.equal(var1.get("commercialTenantId"), orderQueryRequest.getCommercialTenantId()));
            }

            return predicate;
        };
        List<Order> all = orderRepository.findAll(specification);
        Integer actualPayment = 0;//成功    实收金额
        Integer discountsMoney = 0;//成功    优惠金额
        Integer consumeMoney = 0;//成功     消费金额
        Integer actualTui = 0;//退款   实收金额
        int consumeTui = 0;//退款 消费金额
        int discountsTui = 0;//退款 优惠金额
        int total = 0;
        for (Order order : all) {
            if (order.getStatus() == 1 || order.getStatus() == 3) {
                total = total + 1;
            }
            if (order.getStatus() == 1) {
                total = total + 1;
                //成功    实收金额
                if (order.getActualPayment() != null) {
                    actualPayment = actualPayment + order.getActualPayment();
                }
                //成功    优惠金额
                if (order.getDiscountsMoney() != null) {
                    discountsMoney = discountsMoney + order.getDiscountsMoney();
                }
                //成功     消费金额
                if (order.getConsumeMoney() != null) {
                    consumeMoney = consumeMoney + order.getConsumeMoney();
                }
            }
            if (order.getStatus() == 3) {
                //退款   实收金额
                if (order.getActualPayment() != null) {
                    actualTui = actualTui + order.getActualPayment();
                }
                //退款    消费金额
                if (order.getConsumeMoney() != null) {
                    consumeTui = consumeTui + order.getConsumeMoney();
                }
                //退款  优惠金额
                if (order.getDiscountsMoney() != null) {
                    discountsTui = discountsTui + order.getDiscountsMoney();
                }
            }
        }
        int realMoney = actualPayment - actualTui;//最后到商家手里的钱
        int realConsume = consumeMoney - consumeTui;
        int realDiscounts = discountsMoney - discountsTui;
        Map map = new HashMap();
        if (orderQueryRequest.getStatus() != null && 3 == (orderQueryRequest.getStatus())) {
            map.put("realMoney", 0);
        } else {
            map.put("realMoney", (double) realMoney / 100);//实收金额
        }
        map.put("realDiscounts", (double) realDiscounts / 100);//优惠金额
        map.put("realConsume", (double) realConsume / 100);//消费金额
        map.put("total", total);
        map.put("actualTui", (double) actualTui / 100);//退款金额
        List list = new ArrayList();
        list.add(map);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        queryResult.setTotal(1);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }



    //每月一日的凌晨2点自动执行上一个月的汇总
    @Scheduled(cron = "* * 3 1 * ?")
    public void countMonth() {
        CommercialCountMonth();//商户月汇总
        AgentCountMonth();//代理商月汇总

    }


    //商户月汇总
    public void CommercialCountMonth() {
        LOGGER.info("每月凌晨3点CommercialCountMonth方法"+new Date());
        String startTime, endTime;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        startTime = format.format(cal_1.getTime());
        String createTime = sdf.format(cal_1.getTime());
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        endTime = format.format(cale.getTime());
        //TODO
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        //查询所有商户id
        List<CommercialTenant> all = commercialRepository.findAll();
        for (CommercialTenant commercialTenant : all) {
            if (commercialTenant.getId() != null) {
                String commercialTenantId = commercialTenant.getId();
                map.put("commercialTenantId", commercialTenantId);
                //按微信时间汇总
                Map mapConsumeWx = orderTotalMapper.tConsumeCountWx(map);//该商户的订单总金额和条数
                Map mapActualWx = orderTotalMapper.tActualCountWx(map);//实收总金额 和条数
                Map mapTuiWx = orderTotalMapper.tTuiCountWx(map);//退款总金额 和条数
                Map mapDiscountsWx = orderTotalMapper.tDiscountsCountWx(map);//折扣总金额

                OrderTotalMonth orderTotalMonthWx = new OrderTotalMonth();
                orderTotalMonthWx.setCommercialId(commercialTenant.getId());//商户id
                orderTotalMonthWx.setPayWay(1);//微信
                if (mapTuiWx != null) {
                    if (String.valueOf(mapTuiWx.get("tuiNum")) == "null" || String.valueOf(mapTuiWx.get("tuiNum"))==null) {
                        orderTotalMonthWx.setTuiNum("0");
                    } else {
                        orderTotalMonthWx.setTuiNum(String.valueOf(mapTuiWx.get("tuiNum")));
                    }
                    if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null" && String.valueOf(mapTuiWx.get("tuiMoney")) == null) {
                        orderTotalMonthWx.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalMonthWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));
                    }
                }else{
                    orderTotalMonthWx.setTuiNum("0");
                    orderTotalMonthWx.setTuiMoney("0");//退款金额
                }
                if (mapActualWx != null) {
                    if (String.valueOf(mapActualWx.get("total")) == "null" || String.valueOf(mapActualWx.get("total")) == null) {
                        orderTotalMonthWx.setTotal("0");
                    } else {
                        orderTotalMonthWx.setTotal(String.valueOf(mapActualWx.get("total")));
                    }
                    if (String.valueOf(mapActualWx.get("totalMoney")) == "null") {
                        orderTotalMonthWx.setTotalMoney("0");
                    } else {
                        orderTotalMonthWx.setTotalMoney(String.valueOf(mapActualWx.get("totalMoney")));
                    }
                }else{
                    orderTotalMonthWx.setTotal("0");
                    orderTotalMonthWx.setTotalMoney("0");
                }
                if (mapDiscountsWx != null) {
                    if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null" || String.valueOf(mapDiscountsWx.get("discountsMoney")) == null) {
                        orderTotalMonthWx.setDiscountsMoney("0");
                    } else {
                        orderTotalMonthWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));
                    }
                }else{
                    orderTotalMonthWx.setDiscountsMoney("0");
                }
                if (mapConsumeWx != null) {
                    if (String.valueOf(mapConsumeWx.get("consumeMoney")) == "null" || String.valueOf(mapConsumeWx.get("consumeMoney")) == null) {
                        orderTotalMonthWx.setConsumeMoney("0");
                    } else {
                        orderTotalMonthWx.setConsumeMoney(String.valueOf(mapConsumeWx.get("consumeMoney")));
                    }
                }else{
                    orderTotalMonthWx.setConsumeMoney("0");
                }
                String actualMoneyWx = "0";
                String discountsMoneyWx = "0";
                if (mapActualWx != null && mapTuiWx != null) {
                    actualMoneyWx = (String) mapActualWx.get("totalMoney");
                    discountsMoneyWx = (String) mapTuiWx.get("tuiMoney");
                }

                Integer settleAccountsWx = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccountsWx = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccountsWx = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);
                    settleAccountsWx = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);
                    settleAccountsWx = actualMoney + discountsMoney;
                }
                orderTotalMonthWx.setSettleAccounts(String.valueOf(settleAccountsWx));

                orderTotalMonthWx.setCreateTime(createTime);//上个月的第一天
                orderTotalMonthRepository.save(orderTotalMonthWx);//保存到ordertotal表中


                //按支付宝时间汇总
                Map mapConsumeAlipay = orderTotalMapper.tConsumeCountAlipay(map);//该门店的订单总金额和条数
                Map mapActualAlipay = orderTotalMapper.tActualCountAlipay(map);//实收总金额 和条数
                Map mapTuiAlipay = orderTotalMapper.tTuiCountAlipay(map);//退款总金额 和条数
                Map mapDiscountsAlipay = orderTotalMapper.tDiscountsCountAlipay(map);//折扣总金额

                OrderTotalMonth orderTotalMonthAlipay = new OrderTotalMonth();
                orderTotalMonthAlipay.setCommercialId(commercialTenant.getId());//商户id
                orderTotalMonthAlipay.setPayWay(0);//支付宝
                if (mapTuiAlipay != null) {
                    if (String.valueOf(mapTuiAlipay.get("tuiNum")) == "null") {
                        orderTotalMonthAlipay.setTuiNum("0");
                    } else {
                        orderTotalMonthAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("tuiNum")));//退款条数
                    }
                    if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null") {
                        orderTotalMonthAlipay.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalMonthAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                    }
                }else{
                    orderTotalMonthAlipay.setTuiNum("0");
                    orderTotalMonthAlipay.setTuiMoney("0");//退款金额
                }
                if (mapActualAlipay != null) {
                    if (String.valueOf(mapActualAlipay.get("total")) == "null") {
                        orderTotalMonthAlipay.setTotal("0");
                    } else {
                        orderTotalMonthAlipay.setTotal(String.valueOf(mapActualAlipay.get("total")));//总条数
                    }
                    if (String.valueOf(mapActualAlipay.get("totalMoney")) == "null") {
                        orderTotalMonthAlipay.setTotalMoney("0");
                    } else {
                        orderTotalMonthAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("totalMoney")));//实收金额
                    }
                }else{
                    orderTotalMonthAlipay.setTotal("0");
                    orderTotalMonthAlipay.setTotalMoney("0");
                }
                if (mapDiscountsAlipay != null) {
                    if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null") {
                        orderTotalMonthAlipay.setDiscountsMoney("0");
                    } else {
                        orderTotalMonthAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                    }
                }else{
                    orderTotalMonthAlipay.setDiscountsMoney("0");
                }
                if (mapConsumeAlipay != null) {
                    if (String.valueOf(mapConsumeAlipay.get("consumeMoney")) == "null") {
                        orderTotalMonthAlipay.setConsumeMoney("0");
                    } else {
                        orderTotalMonthAlipay.setConsumeMoney(String.valueOf(mapConsumeAlipay.get("consumeMoney")));//订单总金额
                    }
                }else{
                    orderTotalMonthAlipay.setConsumeMoney("0");
                }

                String actualMoney1 = "null";
                String discountsMoney1 = "null";

                if (mapActualAlipay != null) {
                    actualMoney1 = String.valueOf(mapActualAlipay.get("totalMoney"));
                    discountsMoney1 = String.valueOf(mapTuiAlipay.get("tuiMoney"));
                }
                Integer settleAccounts = 0;
                if (actualMoney1 == "null" && discountsMoney1 == "null") {
                    settleAccounts = 0;
                }
                if (actualMoney1 == "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    settleAccounts = discountsMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 == "null") {
                    int actualMoney = Integer.parseInt(actualMoney1);
                    settleAccounts = actualMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    int actualMoney = Integer.parseInt(actualMoney1);
                    settleAccounts = actualMoney + discountsMoney;
                }
                orderTotalMonthAlipay.setSettleAccounts(String.valueOf(settleAccounts));

                orderTotalMonthAlipay.setCreateTime(createTime);//昨天的日期
                orderTotalMonthRepository.save(orderTotalMonthAlipay);//保存到ordertotal表中

                //微信+支付宝的

                OrderTotalMonth orderTotalMonth = new OrderTotalMonth();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalMonthAlipay.getTuiMoney() == null) {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTuiMoney());
                }
                if (orderTotalMonthWx.getTuiMoney() == null) {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalMonthWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalMonthAlipay.getTuiNum() == null) {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTuiNum());
                }
                if (orderTotalMonthWx.getTuiNum() == null) {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalMonthWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalMonthAlipay.getTotal() ==null) {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalMonthAlipay.getTotal());
                }
                if (orderTotalMonthWx.getTotal() == null) {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalMonthWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalMonthAlipay.getTotalMoney() == null) {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTotalMoney());
                }
                if (orderTotalMonthWx.getTotalMoney() == null) {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalMonthWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalMonthAlipay.getDiscountsMoney() == null) {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalMonthAlipay.getDiscountsMoney());
                }
                if (orderTotalMonthWx.getDiscountsMoney() == null) {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalMonthWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalMonthAlipay.getSettleAccounts() == null) {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalMonthAlipay.getSettleAccounts());
                }
                if (orderTotalMonthWx.getSettleAccounts() == null) {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalMonthWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalMonthAlipay.getConsumeMoney() == null) {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalMonthAlipay.getConsumeMoney());
                }
                if (orderTotalMonthWx.getConsumeMoney() == null) {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalMonthWx.getConsumeMoney());
                }
                orderTotalMonth.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotalMonth.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotalMonth.setCreateTime(createTime);
                orderTotalMonth.setCommercialId(commercialTenant.getId());
                orderTotalMonth.setTotal(String.valueOf(totalA + totalW));
                orderTotalMonth.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotalMonth.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotalMonth.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotalMonth.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotalMonth.setPayWay(2);
                orderTotalMonthRepository.save(orderTotalMonth);//商户的微信支付宝日汇总保存到ordertotal表中

            }
        }
    }


    //代理商月汇总
    public void AgentCountMonth() {
        LOGGER.info("每月凌晨3点AgentCountMonth方法"+new Date());
        String startTime, endTime;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        startTime = format.format(cal_1.getTime());
        String createTime = sdf.format(cal_1.getTime());
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        endTime = format.format(cale.getTime());
        //TODO
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);

        //查询所有代理商id
        List<Agent> all = agentRepository.findAll();
        for (Agent agent : all) {
            if (agent.getId() != null) {
                String agentId = agent.getId();
                map.put("agentId", agentId);
                //按微信时间汇总
                Map mapConsumeWx = orderTotalMapper.aConsumeCountWx(map);//该商户的订单总金额和条数
                Map mapActualWx = orderTotalMapper.aActualCountWx(map);//实收总金额 和条数
                Map mapTuiWx = orderTotalMapper.aTuiCountWx(map);//退款总金额 和条数
                Map mapDiscountsWx = orderTotalMapper.aDiscountsCountWx(map);//折扣总金额

                OrderTotalMonth orderTotalMonthWx = new OrderTotalMonth();
                orderTotalMonthWx.setAgentId(agent.getId());//代理商id
                orderTotalMonthWx.setPayWay(1);//微信
                if (mapTuiWx != null) {
                    if (String.valueOf(mapTuiWx.get("tuiNum")) == "null" || String.valueOf(mapTuiWx.get("tuiNum"))==null) {
                        orderTotalMonthWx.setTuiNum("0");
                    } else {
                        orderTotalMonthWx.setTuiNum(String.valueOf(mapTuiWx.get("tuiNum")));
                    }
                    if (String.valueOf(mapTuiWx.get("tuiMoney")) == "null" && String.valueOf(mapTuiWx.get("tuiMoney")) == null) {
                        orderTotalMonthWx.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalMonthWx.setTuiMoney(String.valueOf(mapTuiWx.get("tuiMoney")));
                    }
                }else{
                    orderTotalMonthWx.setTuiNum("0");
                    orderTotalMonthWx.setTuiMoney("0");//退款金额
                }
                if (mapActualWx != null) {
                    if (String.valueOf(mapActualWx.get("total")) == "null" || String.valueOf(mapActualWx.get("total")) == null) {
                        orderTotalMonthWx.setTotal("0");
                    } else {
                        orderTotalMonthWx.setTotal(String.valueOf(mapActualWx.get("total")));
                    }
                    if (String.valueOf(mapActualWx.get("totalMoney")) == "null") {
                        orderTotalMonthWx.setTotalMoney("0");
                    } else {
                        orderTotalMonthWx.setTotalMoney(String.valueOf(mapActualWx.get("totalMoney")));
                    }
                }else{
                    orderTotalMonthWx.setTotal("0");
                    orderTotalMonthWx.setTotalMoney("0");
                }
                if (mapDiscountsWx != null) {
                    if (String.valueOf(mapDiscountsWx.get("discountsMoney")) == "null" || String.valueOf(mapDiscountsWx.get("discountsMoney")) == null) {
                        orderTotalMonthWx.setDiscountsMoney("0");
                    } else {
                        orderTotalMonthWx.setDiscountsMoney(String.valueOf(mapDiscountsWx.get("discountsMoney")));
                    }
                }else{
                    orderTotalMonthWx.setDiscountsMoney("0");
                }
                if (mapConsumeWx != null) {
                    if (String.valueOf(mapConsumeWx.get("consumeMoney")) == "null" || String.valueOf(mapConsumeWx.get("consumeMoney")) == null) {
                        orderTotalMonthWx.setConsumeMoney("0");
                    } else {
                        orderTotalMonthWx.setConsumeMoney(String.valueOf(mapConsumeWx.get("consumeMoney")));
                    }
                }else{
                    orderTotalMonthWx.setConsumeMoney("0");
                }
                String actualMoneyWx = "0";
                String discountsMoneyWx = "0";
                if (mapActualWx != null && mapTuiWx != null) {
                    actualMoneyWx = (String) mapActualWx.get("totalMoney");
                    discountsMoneyWx = (String) mapTuiWx.get("tuiMoney");
                }

                Integer settleAccountsWx = 0;//订单金额
                if (actualMoneyWx == "null" && discountsMoneyWx == "null") {
                    settleAccountsWx = 0;
                }
                if (actualMoneyWx == "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    settleAccountsWx = discountsMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx == "null") {
                    int actualMoney = Integer.parseInt(actualMoneyWx);
                    settleAccountsWx = actualMoney;
                }
                if (actualMoneyWx != "null" && discountsMoneyWx != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoneyWx);
                    int actualMoney = Integer.parseInt(actualMoneyWx);
                    settleAccountsWx = actualMoney + discountsMoney;
                }
                orderTotalMonthWx.setSettleAccounts(String.valueOf(settleAccountsWx));

                orderTotalMonthWx.setCreateTime(createTime);//上个月2020-02
                orderTotalMonthRepository.save(orderTotalMonthWx);//保存到ordertotal表中

                //按支付宝时间汇总
                Map mapConsumeAlipay = orderTotalMapper.aConsumeCountAlipay(map);//该门店的订单总金额和条数
                Map mapActualAlipay = orderTotalMapper.aActualCountAlipay(map);//实收总金额 和条数
                Map mapTuiAlipay = orderTotalMapper.aTuiCountAlipay(map);//退款总金额 和条数
                Map mapDiscountsAlipay = orderTotalMapper.aDiscountsCountAlipay(map);//折扣总金额

                OrderTotalMonth orderTotalMonthAlipay = new OrderTotalMonth();
                orderTotalMonthAlipay.setAgentId(agent.getId());//代理商id
                orderTotalMonthAlipay.setPayWay(0);//支付宝
                if (mapTuiAlipay != null) {
                    if (String.valueOf(mapTuiAlipay.get("tuiNum")) == "null" || String.valueOf(mapTuiAlipay.get("tuiNum")) == null) {
                        orderTotalMonthAlipay.setTuiNum("0");
                    } else {
                        orderTotalMonthAlipay.setTuiNum(String.valueOf(mapTuiAlipay.get("tuiNum")));//退款条数
                    }
                    if (String.valueOf(mapTuiAlipay.get("tuiMoney")) == "null" || String.valueOf(mapTuiAlipay.get("tuiMoney")) == null) {
                        orderTotalMonthAlipay.setTuiMoney("0");//退款金额
                    } else {
                        orderTotalMonthAlipay.setTuiMoney(String.valueOf(mapTuiAlipay.get("tuiMoney")));//退款金额
                    }
                }else{
                    orderTotalMonthAlipay.setTuiNum("0");
                    orderTotalMonthAlipay.setTuiMoney("0");//退款金额
                }
                if (mapActualAlipay != null) {
                    if (String.valueOf(mapActualAlipay.get("total")) == "null" || String.valueOf(mapActualAlipay.get("total")) == null) {
                        orderTotalMonthAlipay.setTotal("0");
                    } else {
                        orderTotalMonthAlipay.setTotal(String.valueOf(mapActualAlipay.get("total")));//总条数
                    }
                    if (String.valueOf(mapActualAlipay.get("totalMoney")) == "null" || String.valueOf(mapActualAlipay.get("totalMoney")) == null) {
                        orderTotalMonthAlipay.setTotalMoney("0");
                    } else {
                        orderTotalMonthAlipay.setTotalMoney(String.valueOf(mapActualAlipay.get("totalMoney")));//实收金额
                    }
                }else{
                    orderTotalMonthAlipay.setTotal("0");
                    orderTotalMonthAlipay.setTotalMoney("0");
                }
                if (mapDiscountsAlipay != null) {
                    if (String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == "null" || String.valueOf(mapDiscountsAlipay.get("discountsMoney")) == null) {
                        orderTotalMonthAlipay.setDiscountsMoney("0");
                    } else {
                        orderTotalMonthAlipay.setDiscountsMoney(String.valueOf(mapDiscountsAlipay.get("discountsMoney")));//折扣金额
                    }
                }else{
                    orderTotalMonthAlipay.setDiscountsMoney("0");
                }
                if (mapConsumeAlipay != null) {
                    if (String.valueOf(mapConsumeAlipay.get("consumeMoney")) == "null" || String.valueOf(mapConsumeAlipay.get("consumeMoney")) == null) {
                        orderTotalMonthAlipay.setConsumeMoney("0");
                    } else {
                        orderTotalMonthAlipay.setConsumeMoney(String.valueOf(mapConsumeAlipay.get("consumeMoney")));//订单总金额
                    }
                }else{
                    orderTotalMonthAlipay.setConsumeMoney("0");
                }

                String actualMoney1 = "null";
                String discountsMoney1 = "null";

                if (mapActualAlipay != null) {
                    actualMoney1 = String.valueOf(mapActualAlipay.get("totalMoney"));
                    discountsMoney1 = String.valueOf(mapTuiAlipay.get("tuiMoney"));
                }
                Integer settleAccounts = 0;
                if (actualMoney1 == "null" && discountsMoney1 == "null") {
                    settleAccounts = 0;
                }
                if (actualMoney1 == "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    settleAccounts = discountsMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 == "null") {
                    int actualMoney = Integer.parseInt(actualMoney1);
                    settleAccounts = actualMoney;
                }
                if (actualMoney1 != "null" && discountsMoney1 != "null") {
                    int discountsMoney = Integer.parseInt(discountsMoney1);
                    int actualMoney = Integer.parseInt(actualMoney1);
                    settleAccounts = actualMoney + discountsMoney;
                }
                orderTotalMonthAlipay.setSettleAccounts(String.valueOf(settleAccounts));

                orderTotalMonthAlipay.setCreateTime(createTime);//昨天的日期
                orderTotalMonthRepository.save(orderTotalMonthAlipay);//保存到ordertotal表中

                //微信+支付宝的

                OrderTotalMonth orderTotalMonth = new OrderTotalMonth();
                int tuiMoneyA = 0;
                int tuiMoneyW = 0;
                if (orderTotalMonthAlipay.getTuiMoney() == null) {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTuiMoney());
                }
                if (orderTotalMonthWx.getTuiMoney() == null) {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalMonthWx.getTuiMoney());
                }

                int tuiNumA = 0;
                int tuiNumW = 0;
                if (orderTotalMonthAlipay.getTuiNum() == null) {
                    tuiMoneyA = 0;
                } else {
                    tuiMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTuiNum());
                }
                if (orderTotalMonthWx.getTuiNum() == null) {
                    tuiMoneyW = 0;
                } else {
                    tuiMoneyW = Integer.parseInt(orderTotalMonthWx.getTuiNum());
                }

                int totalA = 0;
                int totalW = 0;
                if (orderTotalMonthAlipay.getTotal() ==null) {
                    totalA = 0;
                } else {
                    totalA = Integer.parseInt(orderTotalMonthAlipay.getTotal());
                }
                if (orderTotalMonthWx.getTotal() == null) {
                    totalW = 0;
                } else {
                    totalW = Integer.parseInt(orderTotalMonthWx.getTotal());
                }

                int totalMoneyA = 0;
                int totalMoneyW = 0;
                if (orderTotalMonthAlipay.getTotalMoney() == null) {
                    totalMoneyA = 0;
                } else {
                    totalMoneyA = Integer.parseInt(orderTotalMonthAlipay.getTotalMoney());
                }
                if (orderTotalMonthWx.getTotalMoney() == null) {
                    totalMoneyW = 0;
                } else {
                    totalMoneyW = Integer.parseInt(orderTotalMonthWx.getTotalMoney());
                }

                int discountsMoneyA = 0;
                int discountsMoneyW = 0;
                if (orderTotalMonthAlipay.getDiscountsMoney() == null) {
                    discountsMoneyA = 0;
                } else {
                    discountsMoneyA = Integer.parseInt(orderTotalMonthAlipay.getDiscountsMoney());
                }
                if (orderTotalMonthWx.getDiscountsMoney() == null) {
                    discountsMoneyW = 0;
                } else {
                    discountsMoneyW = Integer.parseInt(orderTotalMonthWx.getDiscountsMoney());
                }

                int settleAccountsA = 0;
                int settleAccountsW = 0;
                if (orderTotalMonthAlipay.getSettleAccounts() == null) {
                    settleAccountsA = 0;
                } else {
                    settleAccountsA = Integer.parseInt(orderTotalMonthAlipay.getSettleAccounts());
                }
                if (orderTotalMonthWx.getSettleAccounts() == null) {
                    settleAccountsW = 0;
                } else {
                    settleAccountsW = Integer.parseInt(orderTotalMonthWx.getSettleAccounts());
                }

                int consumeMoneyA = 0;
                int consumeMoneyW = 0;
                if (orderTotalMonthAlipay.getConsumeMoney() == null) {
                    consumeMoneyA = 0;
                } else {
                    consumeMoneyA = Integer.parseInt(orderTotalMonthAlipay.getConsumeMoney());
                }
                if (orderTotalMonthWx.getConsumeMoney() == null) {
                    consumeMoneyW = 0;
                } else {
                    consumeMoneyW = Integer.parseInt(orderTotalMonthWx.getConsumeMoney());
                }
                orderTotalMonth.setTuiNum(String.valueOf(tuiNumA + tuiNumW));
                orderTotalMonth.setTuiMoney(String.valueOf(tuiMoneyA + tuiMoneyW));
                orderTotalMonth.setCreateTime(createTime);
                orderTotalMonth.setAgentId(agent.getId());
                orderTotalMonth.setTotal(String.valueOf(totalA + totalW));
                orderTotalMonth.setTotalMoney(String.valueOf(totalMoneyA + totalMoneyW));
                orderTotalMonth.setDiscountsMoney(String.valueOf(discountsMoneyW + discountsMoneyA));
                orderTotalMonth.setSettleAccounts(String.valueOf(settleAccountsW + settleAccountsA));
                orderTotalMonth.setConsumeMoney(String.valueOf(consumeMoneyW + consumeMoneyA));
                orderTotalMonth.setPayWay(2);
                orderTotalMonthRepository.save(orderTotalMonth);//商户的微信支付宝日汇总保存到ordertotal表中

            }
        }
    }

}
